简体   繁体   English

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

[英]How to move selenium's focus to the new tab window?

I am clicking on a link, which results in new tab. 我点击了一个链接,该链接显示了新标签页。 I want to move to that tab, close that tab, and then again switch back to Parent window. 我想移至该选项卡,关闭该选项卡,然后再次切换回“父窗口”。

I have written following code, its showing error, Actually in my code, control is not moving to child window. 我写了下面的代码,它显示错误,实际上在我的代码中,控件没有移到子窗口。 Please see following code, and help me: 请查看以下代码,并为我提供帮助:

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

You can also use this kind of code to do, switching between tabs. 您也可以使用这种代码来在制表符之间切换。

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());      
}

Control is not moving to child window because you are not capturing the child window's handle and switching to it. 控件没有移到子窗口,因为您没有捕获子窗口的句柄并切换到它。 Once the new tab is opened, you need to find all the windows handle using getWindowHandles() method. 打开新选项卡后,需要使用getWindowHandles()方法查找所有窗口句柄。 The first value in the set returned by getWindowHandles() method will be parent window's handle and the second value will be child window's handle. getWindowHandles()方法返回的集合中的第一个值将是父窗口的句柄,第二个值将是子窗口的句柄。 If you want to perform any operation on child window, you need to first switch to it using driver.switchTo().window() . 如果要在子窗口上执行任何操作,则需要先使用driver.switchTo()。window()切换到该窗口 Following code demonstrates, how we can switch to the new tab and then close it: 以下代码演示了如何切换到新选项卡然后将其关闭:

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.");
}

Let me know, if you have any further queries. 让我知道,如果您还有其他疑问。

You need to take care of a couple of points as follows: 您需要注意以下几点:

  • Within System.setProperty() line the Key is webdriver.chrome.driver System.setProperty()行中, webdriver.chrome.driver
  • Within System.setProperty() line the Value must be the absolute path of the WebDriver variant, ie of chromedriver.exe System.setProperty()行中, 必须是WebDriver变体(即chromedriver.exe的绝对路径
  • To switch() to a new tab , you need to induce WebDriverWait with ExpectedConditions set as numberOfWindowsToBe() . 要将switch()新选项卡 ,您需要通过将ExpectedConditions设置为numberOfWindowsToBe()来诱发WebDriverWait
  • Here is your own code with some simple modifications: 这是经过一些简单修改的​​您自己的代码:

     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); } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM