Selenium, how can I choose a new window

I run my selenium rc test in Eclipse using TestNG. I have a link that is trying to open a new browser page. How can I choose this new page to work? I am using this code:

selenium.selectWindow("name=NewPage");

however, he says the page was not found. I am also trying to determine page ids or headings using this code:

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

It does not detect my new open window:

 win: MainPage win: 

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

I am writing this code selenium.selectWindow("name=blank99157"); , but getting an 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. 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.

+6
source share
4 answers

The window obviously does not have a name, so you cannot select it by name.

  • If the window opens using JavaScript and you can change the script, try changing window.open("someUrl"); on window.open("someUrl", "someName"); , then you can select the window by the given name. Additional info on the MDN doc for window.open() .

  • Selenium RC does not support <a href="someUrl" target="_blank"> links (which open the link in a new window). Therefore, if a window is opened with a link of this type, you should find this <a> element, get the href attribute and call

     selenium.openWindow(theFoundUrl, "theNewWindow"); selenium.selectWindow("id=theNewWindow"); 
  • If it is opened using JavaScript before or during the onload , you need to call

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

    Additional information about this is in SEL-339 or in openWindow() and selectWindow() JavaDocs.

  • If you only have two windows / you want to open the newest, you can try

    selenium.selectPopup()

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

  • 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");

  • Otherwise, you must selenium.getAllWindowNames() over selenium.getAllWindowNames() to get the correct name (Selenium creates names for windows without one). However, you cannot hardcode this name into your test file, because it will change every time, so for this you will need to work out some dynamic logic.

  • You will not like it: go to WebDriver. It should be much more resistant to such problems.

+7
source
 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); 
+1
source

You may not be using the correct window id.

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

Let me know that this helps.

0
source

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

0
source

Source: https://habr.com/ru/post/919095/


All Articles