selenium,我该如何选择新窗口
我在Eclipse中用TestNG运行我的selenium rc测试。 我有一个试图打开新浏览器页面的链接。 如何选择此新页面进行操作? 我用这个代码:
selenium.selectWindow("name=NewPage");
但它说找不到页面。 我还尝试使用以下代码定义页面ID或标题:
String[] wins = selenium.getAllWindowIds(); for (String s : wins) System.out.println("win: " + s);
它没有定义我新打开的窗口:
win: MainPage win:
如果使用selenium.getAllWindowNames()
我win: selenium_main_app_window win: selenium_blank65815
。
我写这段代码selenium.selectWindow("name=blank99157");
但得到错误 – 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.
窗口显然没有名称,因此您无法按名称选择它。
-
如果通过JavaScript打开窗口并且您可以更改脚本,请尝试更改
window.open("https://stackoverflow.com/questions/11220869/selenium-how-can-i-select-new-window/someUrl");
towindow.open("https://stackoverflow.com/questions/11220869/selenium-how-can-i-select-new-window/someUrl", "someName");
,您将可以按设置名称选择窗口。 有关window.open()
的MDN doc的更多信息。 -
Selenium RC不支持
链接(在新窗口中打开链接)。 因此,如果通过此类型的链接打开窗口,则必须找到此
元素,获取
href
属性并调用selenium.openWindow(theFoundUrl, "theNewWindow"); selenium.selectWindow("id=theNewWindow");
-
如果在
onload
事件之前或期间通过JavaScript打开,则需要调用selenium.openWindow("", "theNewWindow"); selenium.selectWindow("id=theNewWindow");
有关此问题的更多信息,请参见错误SEL-339或
openWindow()
和selectWindow()
JavaDocs。 -
如果您只有两个窗口/想要打开最新的窗口,您可以尝试
selenium.selectPopup()
这显然是最简单的方法,因为它选择了第一个非顶部窗口。 因此,它只在您想要选择最新的弹出窗口时才有用。
-
如果新窗口具有唯一标题,则可以执行此操作
selenium.selectPopup("Title of the window");
或
selenium.selectWindow("title=Title of the window");
-
否则,您必须遍历
selenium.getAllWindowNames()
以获取正确的名称(Selenium为没有窗口的窗口创建名称)。 但是,您无法将该名称硬编码到测试用例中,因为它每次都会更改,因此您需要为此计算出一些动态逻辑。 -
你不会喜欢这个:去WebDriver。 它应该对这些问题更具抵抗力。
WebDriver driver = new FirefoxDriver(); WebElement inputhandler = driver.findelement(By.linktext("whatever here")); inputhandler.click(); String parentHandle = driver.getWindowHandle(); Set PopHandle = driver.getWindowHandles(); Iterator 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);
您可能没有使用正确的窗口ID。
看看这个链接。 你可以在这里找到答案。
让我知道你这有帮助。
尝试selenium.getAllWindowNames(),selenium.getAllWindowTitles()..其中一个肯定会起作用。