如何为InternetExplorerDriver设置无代理

时间:2013-08-13 19:11:53

标签: webdriver selenium-webdriver

我的IE浏览器之前是设置代理。我想在运行时初始化一个新的InternetExplorerDriver实例时设置直接连接(无代理)。我可以使用FirefoxProfile但不能使用DesiredCapabilities。下面的代码只是设置了指定的代理,但没有设置代理。 你能帮我设一下InternetExplorerDriver的代理吗?

String PROXY = "localhost:8080";

org.openqa.selenium.Proxy proxy = new org.openqa.selenium.Proxy();
proxy.setHttpProxy(PROXY)
     .setFtpProxy(PROXY)
     .setSslProxy(PROXY);
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability(CapabilityType.PROXY, proxy);
WebDriver driver = new InternetExplorerDriver(cap);

我会有类似的东西:

Proxy proxy = Proxy.NO_Proxy;
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability(CapabilityType.PROXY, proxy);
WebDriver driver = new InternetExplorerDriver(cap);

[编辑]

String ieDriverPath = Constants.PROJECT_PATH + "\\src\\lib\\handler\\IEDriverServer.exe";
        DesiredCapabilities ieCapabilities = new DesiredCapabilities();

        org.openqa.selenium.Proxy proxy = new org.openqa.selenium.Proxy();
        proxy.setProxyType(org.openqa.selenium.Proxy.ProxyType.DIRECT);
        ieCapabilities.setCapability(CapabilityType.PROXY, proxy);

        System.setProperty("webdriver.ie.driver", ieDriverPath);
        WebDriver webDriver = new InternetExplorerDriver(ieCapabilities);
        webDriver.manage().timeouts().implicitlyWait(Constants.SE_WAIT_IN_SECOND, TimeUnit.SECONDS);

上面的代码不起作用。失败如下:

Aug 14, 2013 9:09:57 AM org.openqa.selenium.browserlaunchers.WindowsProxyManager backupRegistrySettings
INFO: Backing up registry settings...
Exception in thread "main" java.lang.RuntimeException: Bug extracting hudsuckr
    at org.openqa.selenium.browserlaunchers.WindowsProxyManager.extractHudsuckr(WindowsProxyManager.java:575)
    at org.openqa.selenium.browserlaunchers.WindowsProxyManager.runHudsuckr(WindowsProxyManager.java:585)
    at org.openqa.selenium.browserlaunchers.WindowsProxyManager.backupHudsuckrSettings(WindowsProxyManager.java:624)
    at org.openqa.selenium.browserlaunchers.WindowsProxyManager.backupRegistrySettings(WindowsProxyManager.java:286)
    at org.openqa.selenium.ie.InternetExplorerDriver.prepareProxy(InternetExplorerDriver.java:296)
    at org.openqa.selenium.ie.InternetExplorerDriver.run(InternetExplorerDriver.java:180)
    at org.openqa.selenium.ie.InternetExplorerDriver.<init>(InternetExplorerDriver.java:174)
    at org.openqa.selenium.ie.InternetExplorerDriver.<init>(InternetExplorerDriver.java:147)
    at acccm.Testing.TID02.main(TID02.java:52)
Caused by: java.io.IOException: Unable to locate: hudsuckr/hudsuckr.exe
    at org.openqa.selenium.io.FileHandler.locateResource(FileHandler.java:86)
    at org.openqa.selenium.io.FileHandler.copyResource(FileHandler.java:55)
    at org.openqa.selenium.browserlaunchers.WindowsProxyManager.extractHudsuckr(WindowsProxyManager.java:572)
    ... 8 more

Selenium version: 2.33
OS: Win 7 64 bit
Browser: IE
Browser version: 8,9

感谢您的帮助。

提前致谢。

2 个答案:

答案 0 :(得分:0)

以下代码适合您:

// WARNING! Untested code written from memory without the
// aid of an IDE. May not run or even compile as written
// here. Modify as necessary.
org.openqa.selenium.Proxy proxy = new org.openqa.selenium.Proxy();
proxy.setProxyType(org.openqa.selenium.Proxy.ProxyType.DIRECT);

DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability(CapabilityType.PROXY, proxy);

// Only needed for Java (not for other languages), and requires
// version 2.35 or higher of WebDriver. Should bypass the Java
// server proxy setting routines and use the native IEDriverServer.exe
// ones.
cap.setCapability("ie.setProxyByServer", true);

WebDriver driver = new InternetExplorerDriver(cap);

答案 1 :(得分:0)

为IE驱动程序设置代理需要在selenium-server-standalone jar中附带的hudsuckr.exe。

将其添加到类路径应该可以解决您的问题。