我正在尝试在Selenium Web驱动程序中切换Windows,但一直收到此错误

Exception in thread "main" org.openqa.selenium.NoSuchWindowException:
Unable  to locate window "{519f3402-9039-4742-8cb3-4106fe8b24c4}"


这是我的代码,我做错了那部分。请帮助

WebDriver driver=new FirefoxDriver();

    WebDriver driver1=new FirefoxDriver();

    driver.get("http://www.facebook.com");

    JavascriptExecutor js = (JavascriptExecutor) driver;

    js.executeScript("window.open()");

    Set<String> WINDOWIDS= driver.getWindowHandles();
    Iterator<String> iter=WINDOWIDS.iterator();
    String Window1=iter.next();//Returns first window id
    String Window2=iter.next();//Returns second window id

    driver.switchTo().window(Window1);

    driver.findElement(By.id("email")).sendKeys("[email protected]");

    driver.findElement(By.id("pass")).sendKeys("[email protected]");

最佳答案

您正在使用driver1.switchTo().window(Window1);,此处Window1是您从驱动程序(而非驱动程序1)获得的窗口句柄。因此,您无法将驱动程序的窗口句柄与driver1一起使用。

尝试为driver1获取单独的句柄,然后仅使用driver1切换到那些窗口集(例如Set<String> WINDOWIDS1= driver1.getWindowHandles();

10-06 05:00