[英]Find a WebElement on the new page using Selenium WebDriver with Java
I'm trying to automate a functionality where I have to open a new tab and start to work on this page, but my code still looking the WebElements on the last Tab.我正在尝试自动化一个功能,我必须打开一个新选项卡并开始在此页面上工作,但我的代码仍在最后一个选项卡上查看 WebElements。 I try to switch the tabs with the follow code but its not working.
我尝试使用以下代码切换选项卡,但它不起作用。
public void DataManager() throws InterruptedException {
ArrayList<String> tabs2 = new ArrayList<String> (driver.getWindowHandles());
driver.switchTo().window(tabs2.get(0));
driver.close();
driver.switchTo().window(tabs2.get(1));
WebElement tes = driver.findElement(By.xpath("//*[@id=\"lui-popover-3\"]/div/ng-transclude/ul/li[2]/button[1]/span[2]"));
tes.click();
}
What's going on, and how can I fix this?这是怎么回事,我该如何解决?
1st time, You have focus driver on 1st tab which is on 0 index and you have close it.第一次,您在 0 索引的第一个选项卡上有焦点驱动程序,并且您已关闭它。 After you close it, Your 1 index become 0 index but you have invoke it on 1 index.
关闭它后,您的 1 索引变为 0 索引,但您已在 1 索引上调用它。
You may Refer sample example:您可以参考示例:
ArrayList<String> tabs= new ArrayList<String>(driver.getWindowHandles());
js.executeScript("window.open()");
driver.switchTo().window(tabs.get(1));
Here, by this lines New tab will be open by keeping current Tab.在这里,通过此行,将通过保留当前选项卡来打开新选项卡。 And driver will invoke for Tab-1.
驱动程序将调用 Tab-1。
I found the solution, I had a problem to find Web Elements in my page, so when I used the following code to change the selenium focus:找到了解决办法,在我的页面中查找Web Elements时遇到了问题,所以当我使用以下代码更改selenium焦点时:
ArrayList<String> tabs2 = new ArrayList<String> (driver.getWindowHandles());
driver.switchTo().window(tabs2.get(1));
And I searched the Web Element using press Tab I found it.我使用按 Tab 键搜索了 Web 元素,我找到了它。
Thanks!谢谢!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.