用selenium和firefox在windows之间切换

时间:2016-03-05 06:49:15

标签: java firefox selenium

所以,我正在尝试自动将电子邮件别名添加到我的Gmail帐户,并且我编写了此代码以将列表中的别名传输到Gmail。但是,我遇到了找到窗口句柄的问题。

我的代码:

        for (int p = 0; p < InstagramAievx.list.getItemCount(); p++) {

        driver.findElement(By.cssSelector("span[role=link][class=sA][idlink='']")).click();
        TimeUnit.SECONDS.sleep(2);
        handleMultipleWindows("Gmail - Add another email address you own");
        driver.findElement(By.id("cfn")).clear();
        driver.findElement(By.name("cfn")).sendKeys(InstagramAievx.gmailname.getText());
        driver.findElement(By.id("focus")).clear();
        driver.findElement(By.name("cfa")).sendKeys(InstagramAievx.list.getItem(m));
        if (driver.findElement(By.id("cfia")).isSelected()) {
            driver.findElement(By.id("cfia")).click();
        }
        ;
        driver.findElement(By.cssSelector("css=[type=submit][value=Next Step »]")).click();

        m++;
        handleMultipleWindows("Settings - " + InstagramAievx.email.getText() + "@gmail.com - Gmail");
    }

}

handleMultipleWindows方法:

    public void handleMultipleWindows(String windowTitle) {
    Set<String> windows = driver.getWindowHandles();

    for (String window : windows) {
        driver.switchTo().window(window);
        if (driver.getTitle().contains(windowTitle)) {
            return;
        }
    }
}

错误:

org.openqa.selenium.NoSuchWindowException: Window not found. The browser window may have been closed.
Command duration or timeout: 24 milliseconds
Build info: version: '2.52.0', revision: '4c2593c', time: '2016-02-11 19:03:33'
System info: host: 'ABJ-PC', ip: '192.168.1.4', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_74'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{applicationCacheEnabled=true, rotatable=false, handlesAlerts=true, databaseEnabled=true, version=44.0.2, platform=WINDOWS, nativeEvents=false, acceptSslCerts=true, webStorageEnabled=true, locationContextEnabled=true, browserName=firefox, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}]
Session ID: 0482ba0b-052c-483a-90f5-2a4d4a55dcf2
*** Element info: {Using=css selector, value=css=[type=submit][value=Next Step »]}
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:678)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:363)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementByCssSelector(RemoteWebDriver.java:492)
    at org.openqa.selenium.By$ByCssSelector.findElement(By.java:430)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:355)
    at com.example.tests.TestInstagram.Gmail(TestInstagram.java:247)
    at com.example.tests.InstagramAievx$11.actionPerformed(InstagramAievx.java:304)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6535)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
    at java.awt.Component.processEvent(Component.java:6300)
    at java.awt.Container.processEvent(Container.java:2236)
    at java.awt.Component.dispatchEventImpl(Component.java:4891)
    at java.awt.Container.dispatchEventImpl(Container.java:2294)
    at java.awt.Component.dispatchEvent(Component.java:4713)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
    at java.awt.Container.dispatchEventImpl(Container.java:2280)
    at java.awt.Window.dispatchEventImpl(Window.java:2750)
    at java.awt.Component.dispatchEvent(Component.java:4713)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.awt.EventQueue$4.run(EventQueue.java:731)
    at java.awt.EventQueue$4.run(EventQueue.java:729)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

有任何帮助吗?感谢。

1 个答案:

答案 0 :(得分:0)

您尝试切换到的新窗口实际上是新选项卡,而不是新窗口。您可以在某些浏览器中使用此方法切换到新选项卡,但不能在FireFox中切换到新选项卡。我建议你在Chrome中使用它。