【问题标题】:how to Customize Firefox proxy settings using selenium如何使用 selenium 自定义 Firefox 代理设置
【发布时间】:2016-05-23 09:54:48
【问题描述】:

面临以下问题,如何设置代理设置?

//sample code to add new proxy settings
firefoxProfile.SetPreference(“network.proxy.http_port”, 8080);

【问题讨论】:

标签: c# selenium selenium-webdriver


【解决方案1】:

查看this answer分享的代码。

//Code copied from the above link
FirefoxProfile profile = new FirefoxProfile();
String PROXY = "xx.xx.xx.xx:8080";
OpenQA.Selenium.Proxy proxy = new OpenQA.Selenium.Proxy();
proxy.HttpProxy=PROXY;
proxy.FtpProxy=PROXY;
proxy.SslProxy=PROXY;
profile.SetProxyPreferences(proxy);
FirefoxDriver driver = new FirefoxDriver(profile);

PROXY 设置为您的代理服务器地址。

Java 用户

    String PROXY = "proxyserver:9999";
    org.openqa.selenium.Proxy proxy = new org.openqa.selenium.Proxy();
    proxy.setHttpProxy(PROXY);
    proxy.setFtpProxy(PROXY);
    proxy.setSslProxy(PROXY);

    org.openqa.selenium.remote.DesiredCapabilities cap = org.openqa.selenium.remote.DesiredCapabilities.firefox();
    cap.setCapability(org.openqa.selenium.remote.CapabilityType.PROXY, proxy);

    org.openqa.selenium.WebDriver driver = new org.openqa.selenium.firefox.FirefoxDriver(cap);

【讨论】:

  • 它对我不起作用,我使用的是代理服务器,而谷歌告诉我的真实 ip 不是我在代理选项中设置的那个。此外,在 C# 中,FirefoxProfile 类已过时。您可以使用FirefoxOptions
【解决方案2】:

什么对我有用:使用 FirefoxOptions 而不是 FirefoxProfile

var options = new FirefoxOptions();
Proxy proxy = new()
{
   HttpProxy = "xxxxxxx"
};
options.Proxy = proxy;
var driver = new FirefoxDriver(options);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-23
    • 1970-01-01
    • 2015-07-29
    • 1970-01-01
    • 2015-06-16
    • 2019-11-06
    • 1970-01-01
    相关资源
    最近更新 更多