I am trying to automate an app in which when I go to a screen and click a button, it closes the current window, and opens new one, which closes again and new one opens. This happens 4 times and then finally I am at the window where the app to be tested it. Selenium WD is not able to recognize the final window when it is opened. Any one has faced this issue in the past?
Not sure but a possible solution would be to tell Selenium to focus on the new open window when it has stopped changing 4 times.
So you wait for the time for your application to open/close all these windows and at the end you do :
for(String winHandle : driver.getWindowHandles()){
driver.switchTo().window(winHandle);
}
This way Selenium should focus on the last open window
Related
As per Difference between webdriver.Dispose(), .Close() and .Quit()
driver.close closes the current active window where driver.quit closes all of the windows. What I get is if I opened a new tab in my current window and then called driver.close the whole windows including my tabs would close. But when I run that code
driver.get("http://testingpool.com/selenium-ide-part-1/");
driver.findElement(By.xpath("//*[#id='content']/div[2]/article/div/div[2]/div[1]/p[8]/span/a")).click(); //opens a new tab
ArrayList<String> allWindowHandles = new ArrayList<> (driver.getWindowHandles());
driver.close()
only the first tabs gets closed. Also I find that my allWindowHandles has length of 2 although I have only one window.
I was trying to open a new window using Selenium as per https://www.tutorialspoint.com/how-to-open-a-new-window-on-a-browser-using-selenium-webdriver-for-python using
((JavascriptExecutor)driver).executeScript("window.open('')");
but that resulted in a new tab not a new window.
I am confused if Selenium differentiates between a tab and a window at all or not.
With the terminology used to distinguish between driver.close() and driver.quit() methods supported by Selenium WebDriver in the post you are referencing to, they actually mostly mean browser tabs, not windows.
By opening a new browser window with
((JavascriptExecutor)driver).executeScript("window.open('')");
or by clicking on some web element opening a new browser tab/window is will normally open a new tab in the existing browser.
What I get is if I opened a new tab in my current window and then
called driver.close the whole window including my tabs would close.
But when I run that code
See you may have opened a new tab by clicking on link or using JavascriptExecutor but Selenium still has focus on first tab of the first window.
Therefore, when you do this:
ArrayList<String> allWindowHandles = new ArrayList<> (driver.getWindowHandles());
driver.close()
It should close the first tab or window, since you have not switched to new tab/window.
Also I find that my allWindowHandles has length of 2
this is cause you have opened a new tab using this line
driver.findElement(By.xpath("//*[#id='content']/div[2]/article/div/div[2]/div[1]/p[8]/span/a")).click(); //opens a new tab
the moment you open a new tab by clicking on a link or Using JavascriptExecutor, you will have +1 windows handle.
However, if you do not switch to any tab or window/s. Selenium will always have focus on first tab of the first window.
Also, window.open('') is Js command not Selenium.
there is a difference between a new tab or windows. But if you take Selenium into consideration, We switch to new tab or windows in a same way.
However to open a new window, you would have to simulate CTRL+N keyboard action.
I have a web application that launches the Save Window (which is Modal) when I click on the Save Button. This window takes uptil 10 seconds to load completely (I can counter this with a wait).
I need to carry out some actions in this window, before I complete the save.
The problem I face is
- The moment the modal window is launched, there is no way I can use a driver.SwitchTo() or driver.Manage().GetAllWindowHandles() etc. I confirmed this with the following lines of Code.
driver.findElement(By.xpath("//*[#id='toolbar']/a[1]")).click();
// After the above line is executed, the Popup gets launched
Set<String> sWindowHandles = driver.getWindowHandles();
System.out.println("Popup");
System.out.println(driver.getWindowHandles().size()); // This always prints "1"
The 3 lines above are not executed at all (or at least not for a long time) until I explicitly close the Popup Window.
How do I work on some control that exists within the Save Window (Modal), when there is no way I can find the Window's handler?
for(String winHandle : driver.getWindowHandles()){
driver.switchTo().window(winHandle);
}
doesn't work, because immediately after the modal window is opened (makes sense to GetWindowHandles only after it is launched), the subsequent lines aren't executed at all.
I'm caught in a deadlock. Please help me out.
If Windows handles is not working on an Application.
Java robot class function can be used.
Robot robot = new Robot();
//Doing a mouse over for the X and Y coordinates of button/link which opens modal window
robot.mouseMove(210,350);
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
robot.delay(100);
//Clicking tab til the cursor is on specific position (textbox/button)
robot.keyPress(KeyEvent.VK_TAB);
robot.delay(100);
//Doing a mouse over for the X and Y coordinates of button/link
robot.mouseMove(300,150);
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
robot.delay(100);
For more information refer the below link.
http://alvinalexander.com/java/java-robot-class-example-mouse-keystroke
I sometimes find the windowhandles can take a while to update with the correct value even though the popup is visible. To counter this I use a loop which breaks when the windowhandles reaches the expected size.
I am new to selenium web driver
I am trying to Login to a webpage which leads to auto close the Login page then open the browser in another window, here when i try to click the Logout button in the new page it is saying the error as "Unable to find element on closed window".
Please some one suggest me in how to handle this situation
Regards,
Pavan
You'll need to switch windows. This link has a good rundown on how you can do this:
How to switch to the new browser window, which opens after click on the button?
The relevant part for you is this:
//Store the current window handle
String winHandleBefore = driver.getWindowHandle();
//Perform the click operation that opens new window
//Switch to new window opened
for(String winHandle : driver.getWindowHandles()){
driver.switchTo().window(winHandle);
}
// Perform the actions on new window
What happens when you try something like this?
driver.switchTo().defaultContent();
Not sure if this the correct solution, but when I tried to run the test case using non - admin instance of Visual Studio 2013 I was getting the Unable to find element on closed window exception. But when I ran the same case as administrator the test case worked as expected and this exception was not thrown.
I am automating using selenium 2.0, my application launches the login page by default in a new window, hence my application has by default two windows. These two windows will remain open always. In this case I could switch between the windows without any problem. The below code is executed without any errors.
for(String winHandle : driver.getWindowHandles()){
driver.switchTo().window(winHandle);
}
The problem starts while clicking the menu options a pop up window launches to search the records. Here, I need to switch between these three windows. I tried the below piece of code. It returns only the first two window handles.
Set availableWindows = driver.getWindowHandles();
This popup window is coded in such a way that, "In a .jsp file it is parameterised as window.open()".
Please let me know, if some one could help me on this?
If you're only seeing 2 window in getWindowHandles(), then the popup is probably a iframe. In this event, use driver.switchTo().frame() to switch focus to that frame instead of looking for an entirely new window.
Here's the documentation on the switch method: http://selenium.googlecode.com/git/docs/api/java/org/openqa/selenium/WebDriver.html#switchTo()
One probable solution is to use JavascriptExecutor.executeScript() method to run a javascript code and handle the pop up window without switching into pop up window.
For example, from parent window of pop up window, run a javascript code which is something like this.
JavascriptExecutor exec = (JavascriptExecutor)driver;exec.executeScript("var popup = <<popupopener function>>; //operate on popup object to manipulate the markup of pop up window");
Scenario is that I need to fill one form and click on a button, as soon as I clicked on the button, one new pop-up should display.
Now my problem is that when I click on a button; 3 or 4 blank windows are opening.
Script just before the error is as follows:
//To click on "Check Availability" Button
waiter.until(ExpectedConditions.presenceOfElementLocated(By.id("ctl00_ContentPlaceHolder1_btnCheckAvailbility")));
driver.findElement(By.id("ctl00_ContentPlaceHolder1_btnCheckAvailbility")).click();
sleep(4000);:
Notes:
When I try to execute manually it works fine
When I run the same script by using Firefox it works fine but unfortunately I have to test this application only on IE.
I properly declared the "IEDriverServer.exe", so that is not an issue.
So ...friends any help or thought on this issue ???
hmmm...
seems a lil bit strange because you press on the button once.
I would recommend you this one approach of handling new windows after button press:
//Store the current window handle
String winHandleBefore = driver.getWindowHandle();
//Perform the click operation that opens new window
//Switch to new window opened
for(String winHandle : driver.getWindowHandles()){
driver.switchTo().window(winHandle);
}
// Perform the actions on new window
//Close the new window, if that window no more required
driver.close();
//Switch back to original browser (first window)
driver.switchTo().window(winHandleBefore);
//continue with original browser (first window)
After much brainstorming I found that if we upgrade our Operating system to Windows 7 then this issue got resolved.