简体   繁体   English

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

[英]How do I set a proxy for firefox using Selenium webdriver with Java?

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");

This code is inside the main method.这段代码在 main 方法中。 When I run this code, firefox is launched but the google url isn't followed and the proxy is not set to the one I specify in the code above.当我运行此代码时,会启动 firefox,但未遵循 google url,并且代理未设置为我在上面的代码中指定的代理。 How can I fix this?我怎样才能解决这个问题?

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);`

Because a bug you cannot use the Proxy object as of now.因为一个错误,你现在不能使用 Proxy 对象。 You should use the below code你应该使用下面的代码

    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");

The bug is discussed on https://github.com/mozilla/geckodriver/issues/764 and you see what the Marionette driver do in background on below link该错误在https://github.com/mozilla/geckodriver/issues/764上讨论,您可以在以下链接中看到 Marionette 驱动程序在后台执行的操作

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

So above code just replicates the same所以上面的代码只是复制了相同的

Work in Selenium 3.14.2, Firefox 62, C# .NET 4.5在 Selenium 3.14.2、Firefox 62、C# .NET 4.5 中工作

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");

you can set proxy details with credentials here in your automation code as above answers explained but another way to do it without sharing your details in your java or python code with firefox profile.您可以在自动化代码中使用凭据设置代理详细信息,如上述答案所述,但另一种方法是无需在您的 java 或 python 代码中与 firefox 配置文件共享您的详细信息。
firefox provides profiles, we can create profile for every user and customize it for proxy ,bookmarks etc. firefox 提供配置文件,我们可以为每个用户创建配置文件,并为代理、书签等自定义它。
windows user : open run(win+R) and type 'firefox -p' windows用户:打开运行(win+R)并输入'firefox -p'
linux user : run command 'firefox -p' linux 用户:运行命令“firefox -p”
1- it will open a dialog box where you can create your profile then select that profile and open firefox. 1- 它将打开一个对话框,您可以在其中创建您的配置文件,然后选择该配置文件并打开 Firefox。
2- open a new tab and search 'about:config' .accept the Risk and Continue then click on show all 2-打开一个新选项卡并搜索“about:config”。接受风险并继续,然后单击“全部显示”
3-here you can search and set all the property 3-在这里您可以搜索和设置所有属性
example:例子:
network.proxy.type 1 network.proxy.type 1
1 for Manually 1 为手动
2 for Automatic proxy 2 为自动代理
for Manuall proxy -对于手动代理 -
property value财产价值
network.proxy.http your proxy ip network.proxy.http 你的代理ip
network.proxy.http_port port number network.proxy.http_port 端口号
network.proxy.ssl your proxy ip network.proxy.ssl 你的代理ip
network.proxy.ssl_port port number network.proxy.ssl_port 端口号
network.proxy.ftp your proxy ip network.proxy.ftp 你的代理ip
network.proxy.ftp_port port number network.proxy.ftp_port 端口号

(to find your profile name ) (查找您的个人资料名称)
Linux : cd .mozilla/firefox/ Linux : cd .mozilla/firefox/
windows: Press.窗户:按。 +R on the keyboard.键盘上的 +R。 A Run dialog will open.将打开一个运行对话框。 Type in: %APPDATA%\\Mozilla\\Firefox\\Profiles\\ Click OK.输入:%APPDATA%\\Mozilla\\Firefox\\Profiles\\ 单击确定。 A window will open containing profile folders now load this profile in java code将打开一个包含配置文件文件夹的窗口,现在在 Java 代码中加载此配置文件

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");

With Java-Selenium version 4.1.1, the following code works for me:使用 Java-Selenium 4.1.1 版,以下代码适用于我:

import org.openqa.selenium.Proxy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;

...

Proxy proxy = new Proxy();
proxy.setHttpProxy("myproxy:80");
proxy.setSslProxy("myproxy:80");

FirefoxOptions firefoxOptions = new FirefoxOptions();
firefoxOptions.setProxy(proxy);

WebDriver driver = new FirefoxDriver(firefoxOptions);

If you like, you can wrap everything up in one line:如果您愿意,可以将所有内容都放在一行中:

WebDriver driver = new FirefoxDriver(new FirefoxOptions().setProxy(new Proxy().setHttpProxy("myproxy:80").setSslProxy("myproxy:80")));

For further proxy settings see: Selenium Proxy API有关更多代理设置,请参阅: Selenium 代理 API

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用selenium webdriver和浏览器HtmlunitDriver for Java设置代理? - How can i set proxy using selenium webdriver with browser HtmlunitDriver for Java? 如何在JAVA中使用Selenium在firefox Webdriver上接受下载提示? - How do I accept the download prompt on the firefox webdriver using selenium in JAVA? 如何使用带有Java的Selenium Webdriver处理允许弹出的Firefox插件 - How do I handle allow pop-up of plug-in for firefox using Selenium Webdriver with Java Selenium WebDriver + Java-如何为Firefox配置代理设置? - Selenium WebDriver + Java - How to configure proxy settings for Firefox? 如何使用 java Selenium WebDriver 下载文件? - How do I download a file using java Selenium WebDriver? 如何使用带有Java的Selenium WebDriver在iframe中拖动对象 - How do I drag an object in an iframe using Selenium WebDriver with Java 如何在Selenium Webdriver 3中为Firefox驱动程序设置默认配置文件? - How can I set a default profile for the Firefox driver in Selenium Webdriver 3? 如何使用 Java 设置 Selenium WebDriver? - How do I setup Selenium WebDriver with Java? 如何使用Java设置Firefox代理? - how to set proxy for firefox using java? 如何使用带有Java的Selenium WebDriver将命令写入Firefox控制台? - How to write commands into the Firefox console using Selenium WebDriver with Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM