在IE11浏览器窗口初始化之后,Selenium直接抛出NoSuchWindowException

时间:2016-07-06 14:23:34

标签: java selenium selenium-webdriver internet-explorer-11 selenium-iedriver

我一直试图在测试会话中运行每种类型的浏览器,不幸的是,这包括IE。浏览器窗口初始化非常好,但是当我尝试通过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的大小,但当这似乎成为问题的根源时,我转而使用工具包。经过进一步检查,我发现它确实是因为某些未知原因找不到驱动程序。

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

我也已将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\8.1\executor-snippets\debug.xml:83: Java returned: 1

1 个答案:

答案 0 :(得分:1)

试试此代码

在IE11中打开一个窗口在setSize之前使用隐式等待

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().window().setSize(new Dimension(1024,768));

包含

import org.openqa.selenium.Dimension;