简体   繁体   English

使用带有 Java 的 Selenium WebDriver 在新页面上查找 WebElement

[英]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.

相关问题 使用带有Java的Selenium Webdriver滚动Webelement - Scrolling Webelement using Selenium Webdriver with Java 如何使用带有Java的Selenium WebDriver将鼠标悬停在Web元素上 - How to mouseover on a webelement using Selenium WebDriver with Java 使用 Selenium WebDriver 和 java 断言 WebElement 不存在 - Assert that a WebElement is not present using Selenium WebDriver with java 排序WebElement列表以查找特定的元素(Selenium WebDriver / Java) - Sorting a WebElement List to find specific Element (Selenium WebDriver/Java) 如何使用Java和Selenium WebDriver识别动态WebElement上传图片 - How to identify dynamic WebElement to upload the picture using Java and Selenium WebDriver 选择列表WebElement的子级-Java-Selenium WebDriver - Select child of List WebElement - Java - Selenium WebDriver Java Selenium Webdriver手动填充列表WebElement - Java Selenium webdriver filling list webelement manually 无法使用Selenium Webdriver通过LinkText查找webElement? - Unable to locate webElement by LinkText using selenium webdriver? Webelement 使用 selenium webdriver 获取空值 - Webelement get null values using selenium webdriver 如何不使用javascriptExecutor在Selenium Webdriver中突出显示Webelement - How to highlight webelement in selenium webdriver not using javascriptExecutor
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM