2

我的 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

我会很感激你的帮助。

提前致谢。

4

2 回答 2

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);
于 2013-08-13T21:20:56.753 回答
0

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

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

于 2013-12-05T06:41:11.770 回答