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.
Related
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
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");
For some of the web links on our page, there are external links which direct the user to say Facebook and Twitter. The links use the HMTL tag target="_blank" so that a new browser tab is opened for our Twitter page.
I would like to verify 1. the new browser tab is open, 2. set focus to it and 3. validate page elements in the new browser tab. The #3 part would be easy enough, once I get focus on it. Tasks #1 and #2 though, I can't figure out, nor can I find anyone talking about this.
Using Selenium2Library (WebDriver)
Selenium does not support tabs (as of June 2013, Selenium 2.33.0) and always opens new windows instead. If your test opens a new tab, good luck to you.
That said, if it correctly opens a new window, use Select Window.
Select Window | url=https://twitter.com/expectedPage
My working WebDriver (2.33.0) code in Java, hopefully it will help a little. The problems you are describing are where my Robot knowledge begins to fall off.
#Test
public void targetBlankLinkTest() {
// load the website and make sure only one window is opened
driver.get(file("TargetBlankLinkTest.html"));
assertEquals(1, driver.getWindowHandles().size());
// click the link and assert that a new window has been opened
driver.findElement(By.linkText("Follow us on Twitter!")).click();
Set<String> windowHandles = driver.getWindowHandles();
assertEquals(2, windowHandles.size());
// switch to the new window and do whatever you like
// (Java doesn't have Switch Window functionality that can select by URL.
// I could write it, but have been using this trick instead)
for (String handle : windowHandles) {
if (handle != driver.getWindowHandle()) {
driver.switchTo().window(handle);
}
}
assertThat(driver.getCurrentUrl(), containsString("twitter.com"));
}
if you want to verify if a new tab has opened or not then you can just compare the window handles before and after clicking a link/element which opens a new tab. Just follow this code might help you.
Here ${LINKDIN} is the element which opens in new tab. so before clicking on it save the window handles in variable and after clicking the element again save the window handle in a variable. now if a new is tab is opened then both the variable value will be different and if no new tab is opened then both variable value is same.in this way you can verify the ne wtab opening.
Go Back
${Current_window} List Windows
Click Element ${LINKDIN}
${New_Windows_list} List Windows
Should Not Be Equal ${Current_window} ${New_Windows_list}
Using Robot Selenium2Library keywords :
#{windows} = List Windows
${numWindows} = Get Length ${windows}
${indexLast} = Evaluate ${numWindows}-1
Should Be True ${numWindows} > 1
Select Window #{windows}[${indexLast}]
Location Should Contain /my/url/whatever
Title Should Be myTitle
Page Should Contain ...
You get the idea.
I recommend:
1. First, acquire new window handle by using the switchTo() method.
2. Then bring that window into focus using a JavaScriptExecutor:
((JavascriptExecutor) myTestDriver).executeScript("window.focus();");
Using switchTo() without a javascript executor to get focus , is not
very reliable in my opinion.
3. Next, do what you need to do in that window.
4. Use driver.close() to close that window.
5. Verify you are back at the parent window with focus.
U can use below code to set focus on new window:
Click element ${a_link_which_opensin_newtab}
Select window New
now u can perform your actions on new tab. If u want to switch back the main tab then use
Select window Main
I am using selenium webdriver + java + eclipse + testng for my automation scripts.
I am trying to get the URL of window which contains an alert box.
On clicking download button on a webpage, it opens an alert box in a new window. I want to fetch the URL of this window.
I tried getCurrentURL command for this but i am getting UnhandledAlertException : Modal dialog present. If i dismiss the alert box the window containing is immediately closed so it is not possible to get the URL.
It seems the alert box (modal dialog here) is blocking webdriver in reading the URL of the window.
Please suggest me a solution for this.
Thanks
I'm not sure I understand the question as an alert box does not have any URL !
Anyhow, you can access it this way : Alert alert = webDriver.switchTo().alert();
You can then retrive the text content or interact with it as described in here : http://selenium.googlecode.com/svn/trunk/docs/api/java/org/openqa/selenium/Alert.html
I think you need to get the set of windowHandles first for this driver.getWindowHandles();. Then get the required Handle of the newly opened window by iterating them. After that you can switch to the opened window using driver.switchTo().window("pass the handle here");
Now your control comes to new window. Then use like driver.getCurrentUrl();
Hope this could help you. Best Regards :)
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.