7

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.

CSharpened
  • 11,674
  • 14
  • 52
  • 86
khris
  • 4,809
  • 21
  • 64
  • 94

4 Answers4

7

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.

Petr Janeček
  • 37,768
  • 12
  • 121
  • 145
  • i try variant 6. Wrote such code: String wins[] = selenium.getAllWindowNames(); for (String s : wins) System.out.println("win: " + s); selenium.selectWindow("name=wins[2]"); – khris Jul 02 '12 at 11:11
  • But get 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. – khris Jul 02 '12 at 11:12
  • @khris Yes, the link in the Exception lies, there are no workarounds mentioned in the docs. You can, however, try the workaround mentioned in my answer in sections 2 and 3, that's possibly the bug you're seeing. – Petr Janeček Jul 02 '12 at 11:48
  • selenium.open("url"); selenium.selectWindow("title"); this works, but this code opens one more window (first is opened after i click my link and second - after those code, actually i need just focus on first, but selenium doesn't see title of my window after i opened it by clicking on link, but see than i open it with url). – khris Jul 02 '12 at 12:07
  • This could be ok, but i have another similar situation, and as new window is opened with clicking on link i need select some record and it automatically is selected in main window. If i just open such popup as new link, i lose all previos inserted data, moreover - popup opens instead main window and data is lost (it may be modified by browser options according open new window in new tab; i use ie and such option is checked, but new window still opens in the same tab that main) – khris Jul 02 '12 at 12:08
  • What about `selenium.openWindow("", "theNewWindow"); selenium.selectWindow("id=theNewWindow");`? Does it pass without the url, or not? It has a special mechanism for this and discovering unattached windows, so it could help. By the way, what does `selenium.selectPopup()` do in your case? It won't help, I'm just curious :). – Petr Janeček Jul 02 '12 at 13:21
  • i get message - The method selectPopup() is undefined for the type DefaultSelenium, if i set some argument like window name or title - it said - window not found – khris Jul 02 '12 at 13:43
  • it's according selenium.selectPopup() – khris Jul 02 '12 at 13:43
  • in this occasion - selenium.openWindow("", "theNewWindow"); selenium.selectWindow("id=theNewWindow"); - ERROR: Window locator not recognized: id – khris Jul 02 '12 at 13:46
  • Okay. After some more research, please try the section 6 with a twist: selenium.openWindow("", "name=" + wins[2]); selenium.selectWindow("name=" + wins[2]); – Petr Janeček Jul 02 '12 at 13:52
  • Object doesn't support this property or method – khris Jul 02 '12 at 14:33
  • selenium.openWindow("", wins[1]); selenium.selectWindow( wins[1]); This have no errors but i gues not work because can't execute following command (which should goes in new window) – khris Jul 02 '12 at 14:39
  • And (without the `openWindow` command), is this the version you tried? Bevause what you wrote: `selenium.selectWindow("name=wins[2]");` is flawed, it should be `selenium.selectWindow("name=" + wins[2]);`. If you did it right and just made a typo in the comment, then I don't know what do do next, I really don't =/. – Petr Janeček Jul 02 '12 at 14:40
  • i wrote as in your example... it is smt weird, so many solution and no one help( – khris Jul 02 '12 at 14:49
  • There is a solution #7, but you won't like it: Go for WebDriver. It should be _far_ more resistant to such problems. – Petr Janeček Jul 02 '12 at 15:07
2
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);
Cache Staheli
  • 3,510
  • 7
  • 32
  • 51
Louise
  • 382
  • 1
  • 6
  • 16
0

You might not be using the correct window ID.

Check out this link. You might find your answer here.

Let me know you this helps.

Hari Reddy
  • 3,808
  • 4
  • 33
  • 42
0

Try selenium.getAllWindowNames(), selenium.getAllWindowTitles()..one of them will work for sure.

Abhishek_Mishra
  • 4,551
  • 4
  • 25
  • 38
  • Use selenium.selectWindow(MainPage) to go that page and selenium.selectWindow(null) to come back to the old page. If that does not work then try this too selenium.openWindow("URL","MyWindow"); selenium.selectWindow("MyWindow"); – Abhishek_Mishra Jun 27 '12 at 13:38