Selenium 在 IE11 浏览器初始化后直接抛出 NoSuchWindowException window

Selenium throwing NoSuchWindowException straight after initialization of IE11 browser window

我一直在尝试 运行 测试会话中的每种浏览器,不幸的是,其中包括 IE。浏览器 window 初始化非常好,但是当我尝试通过 Selenium 调整它的大小时,它立即抛出 "NoSuchWindowException".

    // Set path for Internet Explorer Driver executable
    System.setProperty("webdriver.ie.driver", "./IEDriverServer.exe");

    // Launch InternetExplorerDriver
    DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
    capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true); 
    driver = new InternetExplorerDriver(capabilities);

    driver.get(address);

    Toolkit toolkit = Toolkit.getDefaultToolkit();
    int Width = (int) toolkit.getScreenSize().getWidth();
    int Height = (int)toolkit.getScreenSize().getHeight();
    System.out.println(driver.toString());
    driver.manage().window().setSize(new Dimension(Width,Height));

我原来用的是

    driver.manage().window().maximize(); 

调整 WebDriver 的大小,但当这似乎是问题的根源时,我切换到工具包。经过进一步检查,我发现它确实只是出于某种未知原因找不到驱动程序。

我在进行了更多的堆栈溢出搜索后添加了 capabilities 对象,但这也没有用。

我也把IE11保护模式设置为开启了。

错误信息:

Exception in thread "main" org.openqa.selenium.NoSuchWindowException: Error retrieving window with handle current (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 8 milliseconds
Build info: version: '2.53.0', revision: '35ae25b', time: '2016-03-15 16:57:40'
System info: host: 'WIN-HHUFCN1FEP8', ip: '127.0.0.1', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_66'
Driver info: org.openqa.selenium.ie.InternetExplorerDriver
Capabilities [{browserAttachTimeout=0, ie.enableFullPageScreenshot=true, enablePersistentHover=true, ie.forceCreateProcessApi=false, ie.forceShellWindowsApi=false, pageLoadStrategy=normal, ignoreZoomSetting=false, ie.fileUploadDialogTimeout=3000, version=11, platform=WINDOWS, proxy={proxyType=}, nativeEvents=true, ie.ensureCleanSession=false, elementScrollBehavior=0, ie.browserCommandLineSwitches=, requireWindowFocus=false, browserName=internet explorer, initialBrowserUrl=http://localhost:47406/, javascriptEnabled=true, ignoreProtectedModeSettings=true, enableElementCacheCleanup=true, unexpectedAlertBehaviour=dismiss}]
Session ID: d01a13ab-53e9-46f6-a19e-00d19ea5f34c
    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:422)
    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$RemoteWebDriverOptions$RemoteWindow.setSize(RemoteWebDriver.java:891)
    at Testing.Testing.main(Testing.java:124)
C:\...\NetBeans\Cache.1\executor-snippets\debug.xml:83: Java returned: 1

试试这个代码

在 IE11 中打开 window 后,在设置大小

之前使用隐式等待
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().window().setSize(new Dimension(1024,768));

包括

import org.openqa.selenium.Dimension;