首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用Selenium webdriver和Java为firefox设置代理?

如何使用Selenium webdriver和Java为firefox设置代理?
EN

Stack Overflow用户
提问于 2017-08-22 14:36:42
回答 4查看 14.9K关注 0票数 3
代码语言:javascript
运行
复制
System.setProperty("webdriver.gecko.driver", "E:\\geckodriver-v0.18.0-win64\\geckodriver.exe");
    Proxy p = new Proxy();
    p.setSocksProxy("83.209.94.87:35923");
    DesiredCapabilities cap = new DesiredCapabilities();
    cap.setCapability(CapabilityType.PROXY, p);
    WebDriver driver = new FirefoxDriver(cap);
    driver.get("https://www.google.com.au");

这段代码在main方法中。当我运行这段代码时,firefox被启动了,但是google url没有被跟踪,代理也没有被设置为我在上面代码中指定的那个。我该如何解决这个问题呢?

代码语言:javascript
运行
复制
public static void main(String[] args) throws InterruptedException, IOException, UnsupportedEncodingException {
    while (true) {
    System.setProperty("webdriver.gecko.driver", "E:\\geckodriver-v0.18.0-win64\\geckodriver.exe");
    WebDriver driver;
    String PROXY = "83.209.94.87:35923";
      //Bellow given syntaxes will set browser proxy settings using DesiredCapabilities.
      Proxy proxy = new Proxy();
      proxy.setAutodetect(false);
      proxy.setProxyType(Proxy.ProxyType.MANUAL);
      proxy.setSocksProxy(PROXY);
      DesiredCapabilities cap = new DesiredCapabilities();
      cap.setCapability(CapabilityType.PROXY, proxy);
      //Use Capabilities when launch browser driver Instance.
      driver = new FirefoxDriver(cap);`
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2017-08-23 18:54:35

因为有一个bug,你现在还不能使用代理对象。您应该使用以下代码

代码语言:javascript
运行
复制
    FirefoxProfile profile = new FirefoxProfile();
    profile.setPreference("network.proxy.type", 1);
    profile.setPreference("network.proxy.socks", "83.209.94.87");
    profile.setPreference("network.proxy.socks_port", 35923);

    FirefoxDriver driver = new FirefoxDriver(profile);
    driver.get("https://www.ipinfo.io");

https://github.com/mozilla/geckodriver/issues/764上讨论了这个错误,你可以在下面的链接中看到后台的木偶驱动程序做了什么

https://dxr.mozilla.org/mozilla-central/source/testing/marionette/session.js#155

所以上面的代码只是复制了相同的

票数 3
EN

Stack Overflow用户

发布于 2018-09-25 23:03:28

使用Selenium 3.14.2、Firefox62、C# .NET 4.5

代码语言:javascript
运行
复制
FirefoxDriverService service = FirefoxDriverService.CreateDefaultService(@"GeckoDriver19", "geckodriver.exe");
service.FirefoxBinaryPath = @"C:\Program Files\Mozilla Firefox\firefox.exe";

FirefoxOptions firefoxOptions = new FirefoxOptions();
firefoxOptions.SetPreference("network.proxy.type", 1);
firefoxOptions.SetPreference("network.proxy.socks", "127.0.0.1");
firefoxOptions.SetPreference("network.proxy.socks_port", 1080);

IWebDriver driver = new FirefoxDriver(service, firefoxOptions);

driver.Navigate().GoToUrl("https://www.hbus.com/register");
票数 1
EN

Stack Overflow用户

发布于 2020-02-20 14:27:25

您可以在自动化代码中使用凭据设置代理详细信息,如上所述,这是另一种方法,无需在java或python代码中与firefox配置文件共享您的详细信息。

firefox提供配置文件,我们可以为每个用户创建配置文件,并为代理、书签等自定义配置文件。

windows用户:打开运行(win+R)并键入'firefox -p'

linux用户:运行命令'firefox -p'

1-它将打开一个对话框,您可以在其中创建您的配置文件,然后选择该配置文件并打开firefox。

2-打开一个新选项卡,搜索.accept 'about:config' Risk并继续,然后单击show all

3-您可以在此处搜索和设置所有属性

示例:

network.proxy.type 1

1表示手动

2表示自动代理

对于手动代理-

network.proxy.http 你的代理ip

network.proxy.ftp 你的代理ip

(查找您的配置文件名称)

Linux:光盘.mozilla/firefox/

键入:%APPDATA%\Mozilla\Firefox\Profiles\单击确定。将打开一个包含配置文件文件夹的窗口,现在在java代码中加载此配置文件

代码语言:javascript
运行
复制
FirefoxOptions options = new FirefoxOptions();
options.setUnhandledPromptBehaviour(UnexpectedAlertBehaviour.ACCEPT);
FirefoxProfile profile=new FirefoxProfile(new File("path of your profile"));
options.setProfile(profile);
WebDriver driver = new FirefoxDriver(options);
System.setProperty("webdriver.gecko.driver", "path of gecko driver");
driver.get("url");
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45810476

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档