Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

selenium, how can I select new window

I run my selenium rc test in Eclipse with TestNG. I have a link which tries to open a new browser page. How can I select this new page to operate in? I use this code:

selenium.selectWindow("name=NewPage");

however it says page not found. I also try to define page ids or titles with this code:

String[] wins = selenium.getAllWindowIds();
    for (String s : wins)
         System.out.println("win: " + s); 

It does not define my new opened window:

win: MainPage
win: 

If use selenium.getAllWindowNames() I get win: selenium_main_app_window win: selenium_blank65815.

I write this code selenium.selectWindow("name=blank99157"); but get the error - ERROR: Window does not exist. If this looks like a Selenium bug, make sure to read http://seleniumhq.org/docs/02_selenium_ide.html#alerts-popups-and-multiple-windows for potential workarounds.

like image 466
khris Avatar asked Jun 27 '12 06:06

khris


2 Answers

The window obviously has no name, so you can't select it by name.

  1. If the window is opened via JavaScript and you can change the script, try changing window.open("someUrl"); to window.open("someUrl", "someName");, you'll be then able to select the window by the set name. More information on the MDN doc for window.open().

  2. Selenium RC doesn't support <a href="someUrl" target="_blank"> links (which open the link in a new window). Therefore, if the window is opened by a link of this type, you have to find this <a> element, get the href attribute and call

    selenium.openWindow(theFoundUrl, "theNewWindow");
    selenium.selectWindow("id=theNewWindow");
    
  3. If it is opened via JavaScript before or during the onload event, you'll need to call

    selenium.openWindow("", "theNewWindow");
    selenium.selectWindow("id=theNewWindow");
    

    More information on this in the bug SEL-339 or in the openWindow() and selectWindow() JavaDocs.

  4. If you have only two windows / want to open the newest one, you can try

    selenium.selectPopup()

    That is, obviously, the easiest way, because it selects the first non-top window. Therefore, it's only useful when you want to select the newest popup.

  5. If the new window has a unique title, you can do

    selenium.selectPopup("Title of the window");
    

    or selenium.selectWindow("title=Title of the window");

  6. Otherwise, you must iterate over selenium.getAllWindowNames() to get the right name (Selenium creates names for windows without one). However, you can't hardcode that name into your testcase, because it will change every time, so you'll need to work out some dynamic logic for this.

  7. You won't like this: Go for WebDriver. It should be far more resistant to such problems.

like image 64
Petr Janeček Avatar answered Oct 16 '22 06:10

Petr Janeček


WebDriver driver = new FirefoxDriver();
WebElement inputhandler = driver.findelement(By.linktext("whatever here"));
inputhandler.click();   
String parentHandle = driver.getWindowHandle();
Set<String> PopHandle = driver.getWindowHandles();
Iterator<String> it = PopHandle.iterator();
String ChildHandle = "";
while(it.hasNext())
{   
    if (it.next() != parentHandle)
    {   
        ChildHandle = it.next().toString();
        // because the new window will be the last one opened
    }
}
driver.switchTo().window(ChildHandle);
WebDriverWait wait1 = new WebDriverWait(driver,30);
wait1.until(ExpectedConditions.visibilityOfElementLocated(By.id("something on page")));

// do whatever you want to do in the page here

driver.close();
driver.switchTo().window(parentHandle);
like image 45
Louise Avatar answered Oct 16 '22 08:10

Louise