如何使用 Selenium WebDriver 在 Firefox 中设置代理?
我们可以使用 Selenium webdriver 在 Firefox 中设置代理。即使存在多层网络,代理服务器也允许用户为测试目的访问应用程序的 URL。
可以在 FirefoxOptions 类的帮助下设置 Firefox 中的代理。端口信息和代理服务器主机将被添加到此类。还可以通过在 FirefoxProfile 类中配置 Firefox 配置文件来完成代理服务器的设置。
示例
使用 FirefoxOptions 的代码实现
import org.openqa.selenium.Proxy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
public class ConfigureProxy {
public static void main(String[] args) {
//object of Proxy
Proxy p = new Proxy();
//adding host and port
p.setHttpProxy("<HOST:PORT>");
p.setSslProxy("<HOST:PORT>");
p.setSslProxy("<HOST:PORT>");
p.setFtpProxy("<HOST:PORT>");
//object of FirefoxOptions
FirefoxOptions o = new FirefoxOptions();
o.setCapability("proxy", p);
//passing Firefox option to webdriver object
WebDriver driver = new FirefoxDriver(o);
//url launch
driver.get("https://tutorialspoint.com/index.htm");
//browser close
driver.close();
}
}示例
使用 FirefoxOptions 的代码实现
import org.openqa.selenium.Proxy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
public class ConfigureProxyProfile {
public static void main(String[] args) {
//object of Proxy
Proxy p = new Proxy();
//adding host and port
p.setHttpProxy("<HOST:PORT>");
p.setSslProxy("<HOST:PORT>");
p.setSslProxy("<HOST:PORT>");
p.setFtpProxy("<HOST:PORT>");
//object of FirefoxProfile
FirefoxProfile pl = new FirefoxProfile();
pl.setProxyPreferences(p);
//passing Firefox profile to webdriver object
WebDriver driver = new FirefoxDriver(pl);
//url launch
driver.get("https://tutorialspoint.com/index.htm");
//browser close
driver.close();
}
}
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP