如何将硒的焦点移到新的选项卡窗口?

时间:2018-08-03 04:02:42

标签: java selenium selenium-webdriver window-handles webdriverwait

我点击了一个链接,该链接显示了新标签页。我想移至该选项卡,关闭该选项卡,然后再次切换回“父”窗口。

我写了下面的代码,它显示错误,实际上在我的代码中,控件没有移到子窗口。请查看以下代码,并为我提供帮助:

WebDriver driver;
System.setProperty("Webdriver.chrome.driver", "chromedriver.exe");
driver = new ChromeDriver();
driver.get("https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_win_open");
String parent_window = driver.getWindowHandle();
driver.switchTo().frame("iframeResult");
driver.findElement(By.xpath("//button[contains(text(),'Try it')]")).click();
Thread.sleep(4000);
String child_window = driver.getWindowHandle();
System.out.println(parent_window);
System.out.println(child_window);

3 个答案:

答案 0 :(得分:0)

您也可以使用这种代码来在制表符之间切换。

public static void main(String[] args) throws Exception {
System.setProperty("webdriver.chrome.driver", "Location of chromedriver exe file");

System.out.println("Ready to launch the browser");
WebDriver driver = new ChromeDriver();
driver.get("https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_win_open");
driver.switchTo().frame("iframeResult");
driver.findElement(By.xpath("//button[contains(text(),'Try it')]")).click();
Thread.sleep(4000);

  //store parent window value in string
  String parentWindow = driver.getWindowHandle();

  //store the set of all windows
  Set<String> allwindows= driver.getWindowHandles();

  for (String childWindow : allwindows) {
    if(!childWindow.equals(parentWindow))
      {
        driver.switchTo().window(childWindow);
        System.out.println("child window");
        System.out.println(driver.getTitle());      
        // do some operation
        //Closing the Child Window.
         driver.close();    
     }
    }
    driver.switchTo().window(parentWindow);
    System.out.println("Parent window");
    System.out.println(driver.getTitle());      
}

答案 1 :(得分:0)

控件没有移到子窗口,因为您没有捕获子窗口的句柄并切换到它。打开新标签后,您需要使用 getWindowHandles()方法查找所有窗口句柄。由 getWindowHandles()方法返回的集合中的第一个值将是父窗口的句柄,第二个值将是子窗口的句柄。如果要在子窗口上执行任何操作,则需要先使用 driver.switchTo()。window()切换到子窗口。以下代码演示了如何切换到新选项卡然后将其关闭:

WebDriver driver;
System.setProperty("Webdriver.chrome.driver", "chromedriver.exe");
driver = new ChromeDriver();
driver.get("https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_win_open");

driver.switchTo().frame("iframeResult");
driver.findElement(By.xpath("//button[contains(text(),'Try it')]")).click();
try {
    Thread.sleep(4000);
} catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

Set<String> handles = driver.getWindowHandles();

if (handles.size() == 2) {
    Iterator<String> itr = handles.iterator(); 

    String parent_window = itr.next().toString();
    String child_window = itr.next().toString();
    System.out.println(parent_window);
    System.out.println(child_window);

    // switching to child window
    driver.switchTo().window(child_window);

    // closing child window
    driver.close();

    // switching back to parent window
    driver.switchTo().window(parent_window);

} else {
    System.out.println("New tab did not open.");
}

如果您还有其他疑问,请告诉我。

答案 2 :(得分:0)

您需要注意以下几点:

  • System.setProperty()行中,webdriver.chrome.driver
  • System.setProperty()行中, Value 必须是 WebDriver 变体的绝对路径,即chromedriver.exe
  • switch()新选项卡,您需要在 ExpectedConditions 设置为numberOfWindowsToBe()的情况下诱发 WebDriverWait
  • 这是您自己的代码,进行了一些简单的修改:

    WebDriver driver;
    System.setProperty("webdriver.chrome.driver", "C:\\path\\to\\chromedriver.exe");
    driver = new ChromeDriver();
    driver.get("https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_win_open");
    String parent_window = driver.getWindowHandle();
    driver.switchTo().frame("iframeResult");
    driver.findElement(By.xpath("//button[contains(text(),'Try it')]")).click();
    new WebDriverWait(driver,10).until(ExpectedConditions.numberOfWindowsToBe(2));
    Set<String> windows = driver.getWindowHandles();
    for(String child_window:windows)
        if(!parent_window.equals(child_window))
        {
            driver.switchTo().window(child_window);
            System.out.println(child_window);
        }