I'm trying to use Selenium to test out a form on a website that consists of multiple pages. There are modals that are suppose to appear on some of the pages based on options that are selected. Currently I'm having trouble getting Selenium to work properly with Fireforx and the modals. Chrome acts as expected, and I haven't bothered with I.E. yet.
When I work through the pages manually with Firefox, everything works as expected.
When I run my Selenium script the modal displays like a Windows pop-up window, not a modal.
I'm using driver.switchTo().alert().accept(); to handle the modals, and the first modal I encounter will close, but once I get to the next page the Selenium code is unable to find any of the elements on the page.
Here is the code I use to click a button:
public void pushButton(String[] values) {
System.out.println("\t Click (" + values[1] + ")");
setLocator(values[0], values[1]);
try {
clickWhenReady(locator).click();
} catch (Exception e) {
System.out.println("Could not find id or xpath value: " + values[1]);
e.printStackTrace();
}
}
private void setLocator(String byType, String value) {
if(byType.toUpperCase().equals("ID")) {
locator= By.id(value);
} else if(byType.toUpperCase().equals("XPATH")){
locator= By.xpath(value);
}
}
private WebElement whenReady(By locator){
WebElement element = (new WebDriverWait(driver, 30))
.until(ExpectedConditions.presenceOfElementLocated(locator));
return element;
}
private WebElement clickWhenReady(By locator){
WebElement element = (new WebDriverWait(driver, 30))
.until(ExpectedConditions.elementToBeClickable(locator));
return element;
}
First of all, modals are not alerts so, driver.switchTo().alert().accept(); will probably not going to buy you anything.
The real issue could be the modals take longer to fade out and blocking Selenium to interact with the page. Your best bet will be to try waiting for the modal to completely disappear from the dom and then try interacting with the elements. For that you can use invisibilityOfElementLocated of ExpectedConditions or similar mechanism. See this
Related
I am trying to automate test cases. It's difficult since the pop message appears at uncertain time as a result the test case fails. Sometimes pop-up appears without a click and other times it is 5-6 clicks before the pop-up appears. I can't locate the pop-up there is no id or XPath.
If popup is windows based then use AutoIT library.
If it is web popup then you can handle it by following code
Set<String> set = driver.getWindowHandles();
List<String> list = new ArrayList<>(set);
// store your main window handle in variable
String mainWindow = list.get(0);
// To close all unwanted popup
for(int i =1; i <list.size(); i++)
{
String unwantedPopup = list.get(i);
driver.switchTo().window(unwantedPopup);
driver.close();
}
// Switch back to your main window
driver.switchTo().window(mainWindow);
I found solution that works for me.
Thread.sleep(5000); //wait for the modal message to appear
String winHandleBefore = driver.getWindowHandle();
driver.findElement(By.xpath("xpath")).click();
Thread.sleep(2000);
driver.switchTo().window(winHandleBefore);
I'm working on a test script that works with Appium (Selenium) on Java for mobile devices.
In the script, there's following codes:
_driver2.swipe(startXL, startYL, endXL, endYL, 100);
and
_driver2.tap(1, startX, startY, 100);
These methods swipe a game object or click on a button in a game.
Here are some details:
Swiping method is using absolute locations on screen. Tap method is using a location that is calculated by an image recognition function.
Here's my question:
While the test is running; if device's screen is different than expected, like push notifications or native dialogs, test doesn't stop and still tries to find an image/location and then clicks/swipes.
I want to make it stop if there's a different screen than expected or the game object didn't move.
My question is probably not clear but I can give you details comment by comment.
Thank you in advance.
You can use an simple if else condition
If element is found { Swipe }
Else { Perform other action }
Step 1:
public boolean isElementFound(String element)
{
try{
WebElement webElement = driver.findElement(By.xpath(element));
System.out.println("isElementFound : true :"+element);
}catch(NoSuchElementException e){
System.out.println("isElementFound : false :"+element);
return false;
}
Step 2 : while(isElementFound(String element))
{
TouchAction touchAction = new TouchAction(appiumDriver);
System.out.println(startx+" "+starty);
System.out.println("Entering swipe");
System.out.println("Swipe from "+startx +" " +starty +"to" +endx +" " +endy );
touchAction.press(startx, starty).waitAction(duration).moveTo(endx,endy).release().perform();
// WebElement webElement = appiumDriver.findElement(By.xpath(element));
webElement.click();
}
I can switch between two tabs/windows but my requirement is to know or get active window between them.
In my project, on a click of a webElement of a page a random pop(tab/window) gets opened and I would like to know whether that(new) window has focus or my original page.
I tried to use JNA Api to get the active window and its title but my web page is
remotely located.
Perfect solution is greatly appreciated.
Thanks
driver.getTitle() will give you the title of the page that you can use to determine which page you are on or if you are on the page where you want to be and then use the logic to switch window if required. getTitle() returns a String and you can use one of the string methods to compare the title, for example:
String title = getDriver().getTitle();
if(!title.equals("Expected Title")) {
//may be you would like to switch window here
}
String title = driver.getTitle()
This will give you the title of the page which you can refer to using Selenium to figure out which page the driver is currently on.
I wrote my own method to switch to a window if the window title is known, maybe some of this would be helpful. I used Selenide (Java) methods for this, but if you've got Vanilla WebDriver, you can achieve the same thing
/** Switches user to window of user's choice */
public static void switchToWindow(String windowTitle) {
WebDriver driver = getWebDriver();
// Get list of all open tabs - note behaviour may be different between FireFox and Chrome.
ArrayList<String> tabs = new ArrayList<>(driver.getWindowHandles());
// iterate through open tabs. If the title of the page is contained in the tab, switch to it.
for (String windowHandle : driver.getWindowHandles()) {
String title = getWebDriver().getTitle();
driver.switchTo().window(windowHandle);
if (title.equalsIgnoreCase(windowTitle)) {
break;
}
}
}
This method might not be lightening fast, but it will iterate through current open windows and check the title matches the one you've specified.
If you want to assert the title, you could use the xpath selector:
String pageTitle = driver.findElement(By.xpath("//title[text() = 'Title you looking for']"));
This is a dumb example with you can surround with try/catch, implement assertions or other technique to have the result you need.
In JavaScript, for me this worked. After clicking on the first link of bing search results in edge, my link opened in a new tab. I explicitly mentioned to stay in the same tab.
async function switchTab() {
await driver.getAllWindowHandles().then(async function (handles) {
await driver.switchTo().window(handles[1]);
});
}
//get initial window handles
Set<String> prevWindowHandles = driver.getWindowHandles();
while(true){
//get current window handles
Set<String> currWindowHandles = driver.getWindowHandles();
//if one of the current window handles not equals to
//any of the previous window handles,switch to this window
//and prevWindowHandles = currWindowHandles
for(String prevHandle : prevWindowHandles){
int noEqualNum = 0;
for(String currHandle : currWindowHandles){
if(!currHandle.equals(prevHandle))
noEqualNum++
}
if(noEqualNum == currWindowHandles.size()){
driver.switchTo().window(currWindow);
prevWindowHandles = currWindowHandles;
break;
}
}
}
String parentHandle = driver.getWindowHandle();
driver.findElement(By.id("ImageButton5")).click();
for (String winHandle : driver.getWindowHandles()) {
driver.switchTo().window(winHandle);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
driver.findElement(By.id("txtEnterDescription")).sendKeys("Test");
driver.findElement(By.id("chklstAllprocedure_0")).click();
I used this code and I got the error as
"Exception in thread "main"
org.openqa.selenium.NoSuchElementException: Unable to find element
with id == txtEnterDescription (WARNING: The server did not provide
any stacktrace information) Command duration or timeout: 30.05
seconds". The HTML code for this text box is ""
Please help me out of this
you may face "NoSuchElementException" only in two case for sure.
1.The Element is yet to be Loaded
- Have an appropriate wait logic here.
2. You may try to locate the element wrongly .
- Double Check your Xpath/ID(what ever)
- Make sure you are in the same frame where the element is present.If not, switch to the frame then.
Just make sure you are switching to the right window
Reason 1 : Just make sure you are switching to the right window
I have an utility method to switch to the required window as shown below
public class Utility
{
public static WebDriver getHandleToWindow(String title){
//parentWindowHandle = WebDriverInitialize.getDriver().getWindowHandle(); // save the current window handle.
WebDriver popup = null;
Set<String> windowIterator = WebDriverInitialize.getDriver().getWindowHandles();
System.err.println("No of windows : " + windowIterator.size());
for (String s : windowIterator) {
String windowHandle = s;
popup = WebDriverInitialize.getDriver().switchTo().window(windowHandle);
System.out.println("Window Title : " + popup.getTitle());
System.out.println("Window Url : " + popup.getCurrentUrl());
if (popup.getTitle().equals(title) ){
System.out.println("Selected Window Title : " + popup.getTitle());
return popup;
}
}
System.out.println("Window Title :" + popup.getTitle());
System.out.println();
return popup;
}
}
It will take you to desired window once title of the window is passed as parameter. In your case you can do.
Webdriver childDriver = Utility.getHandleToWindow("titleOfChildWindow");
and then again switch to parent window using the same method
Webdriver parentDriver = Utility.getHandleToWindow("titleOfParentWindow");
This method works effectively when dealing with multiple windows
Resaon 2 : wait for the element
WebdriverWait wait = new WebdriverWait(driver,7000);
wait.until(ExpectedConditions.visbilityOfElementLocatedBy(By.name("nameofElement")));
Reason 3 : check if the element is in a frame if yes switch to the frame before
driver.switchTo.frame("frameName");
Let know if it works ..
Guess the problem is in your switch to logic. I think when you are switching it is passing the code of the parent instead of the child. For this scenario create two local variables parent and child. Loop through the window handles using an iterator and set the parent and child window id's to the variables and pass the child id to the switchTo method. This should work. Keep me posted. Happy coding.
I am using Selenium WebDriver to automate my browser tests. My browser header is floating and is always present irrespective of the browser scroll.
So when I click on certain elements that are present below the current visible region of the browser, selenium tries to scroll the element into view and click them.
But because of the auto scrolling as such the elements are scrolled behind the floating header and when any action is performed on them, the elements in the page header get clicked.
is there any way to limit the default scroll of the WebDriver?
Locatable hoverItem = (Locatable) driver.findElement(By.xpath("//li[text()='Reklama w Google']"));
int y = hoverItem.getCoordinates().getLocationOnScreen().getY();
((JavascriptExecutor)driver).executeScript("window.scrollBy(0,"+y+");");
If you want to scroll on the firefox window using selenium webdriver, one of the way is to use javaScript in the java code, The javeScript code to scroll down is as follows:
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("window.scrollTo(0,Math.max(document.documentElement.scrollHeight," +
"document.body.scrollHeight,document.documentElement.clientHeight));");
You can scroll to the necessary location using javascript You need to use the scrollTo method rather than the scrollBy method for it to work.
public void scrollToElement(By by) {
Locatable element = (Locatable) selenium.findElement(by);
Point p= element.getCoordinates().getLocationOnScreen();
JavascriptExecutor js = (JavascriptExecutor) selenium;
js.executeScript("window.scrollTo(" + p.getX() + "," + (p.getY()+150) + ");");
}
Scroll to top can be done:
private void scrollToTop() {
JavascriptExecutor js = (JavascriptExecutor) webDriver;
js.executeScript("window.scrollTo(0, 0);");
}
Simple use the
.sendKeys(Keys.PAGE_DOWN);
when your element was visible, just click on it, by .click(element).perform();
for me work something like this:
clicker = new Actions(driver);
clicker.sendKeys(Keys.PAGE_DOWN);
Thread.sleep(1000);
clicker.click(button).perform();
Thread.sleep(1000);
For scrolling down:
System.setProperty("webdriver.chrome.driver",
"/home/shreetesh/chromedriver");
WebDriver driver = new ChromeDriver();
String url = "https://en.wikipedia.org/wiki/Main_Page";
driver.get(url);
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("scroll(0, 25000);");
To scrolling up just replace the value of scroll with (2500, 0).
Use below code for scrolling up and scrolling down
Actions dragger = new Actions(driver);
WebElement draggablePartOfScrollbar = driver.findElement(By.xpath("<Scroll bar Element >"));
// drag downwards
int numberOfPixelsToDragTheScrollbarDown = 50;
for (int i=10 ; i<500 ; i=i+numberOfPixelsToDragTheScrollbarDown) {
try {
// this causes a gradual drag of the scroll bar, 10 units at a time
dragger.moveToElement(draggablePartOfScrollbar).clickAndHold().moveByOffset(0,numberOfPixelsToDragTheScrollbarDown).release().perform();
Thread.sleep(1000L);
} catch(Exception e1){}
}
// now drag opposite way (downwards)
numberOfPixelsToDragTheScrollbarDown = -50;
for (int i=500;i>10;i=i+numberOfPixelsToDragTheScrollbarDown){
// this causes a gradual drag of the scroll bar, -10 units at a time
dragger.moveToElement(draggablePartOfScrollbar).clickAndHold().moveByOffset(0,numberOfPixelsToDragTheScrollbarDown).release().perform();
Thread.sleep(1000L);
}
I recently had this problem due to a Drupal menu blocking the element when I ran this code:
public void scrollTo(WebElement x) {
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", x);
}
After referencing this page, I updated to set the boolean to false using this code, and it works great:
public void scrollTo(WebElement x) {
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(false);", x);
}