title: “Http proxies”
weight: 7
A proxy server acts as an intermediary for
requests between a client and a server. In simple,
the traffic flows through the proxy server
on its way to the address you requested and back.
A proxy server for automation scripts
with Selenium could be helpful for:
- Capture network traffic
- Mock backend calls made by the website
- Access the required website under complex network
topologies or strict corporate restrictions/policies.
If you are in a corporate environment, and a
browser fails to connect to a URL, this is most
likely because the environment needs a proxy to be accessed.
Selenium WebDriver provides a way to proxy settings
{{< code-tab >}}
{{< code-panel language=“java” >}}
import org.openqa.selenium.Proxy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
public class proxyTest {
public static void main(String args) {
Proxy proxy = new Proxy();
proxy.setHttpProxy(“HOST:PORT”);
ChromeOptions options = new ChromeOptions();
options.setCapability(“proxy”, proxy);
WebDriver driver = new ChromeDriver(options);
driver.get(“https://www.google.com/”);
driver.manage().window().maximize();
driver.quit();
}
}
{{< / code-panel >}}
{{< code-panel language=“python” >}}
from selenium import webdriver
PROXY = “HOST:PORT”
webdriver.DesiredCapabilities.FIREFOX[‘proxy’] = {
“httpProxy”: PROXY,
“ftpProxy”: PROXY,
“sslProxy”: PROXY,
“proxyType”: “MANUAL”,
}
with webdriver.Firefox() as driver:
# Open URL
driver.get(“https://selenium.dev”)
{{< / code-panel >}}
{{< code-panel language=“csharp” >}}
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
public class ProxyTest{
public static void Main() {
ChromeOptions options = new ChromeOptions();
Proxy proxy = new Proxy();
proxy.Kind = ProxyKind.Manual;
proxy.IsAutoDetect = false;
proxy.SslProxy = “HOST:PORT”;
options.Proxy = proxy;
options.AddArgument(“ignore-certificate-errors”);
IWebDriver driver = new ChromeDriver(options);
driver.Navigate().GoToUrl(“https://www.selenium.dev/”);
}
}
{{< / code-panel >}}
{{< code-panel language=“ruby” >}}
this code was written with Selenium 4
proxy = Selenium::WebDriver::Proxy.new(http: ‘HOST:PORT’)
cap = Selenium::WebDriver::Remote::Capabilities.chrome(proxy: proxy)
driver = Selenium::WebDriver.for(:chrome, capabilities: cap)
driver.get(‘http://google.com’)
{{< / code-panel >}}
{{< code-panel language=“javascript” >}}
let webdriver = require(‘selenium-webdriver’);
let chrome = require(‘selenium-webdriver/chrome’);
let proxy = require(‘selenium-webdriver/proxy’);
let opts = new chrome.Options();
(async function example() {
opts.setProxy(proxy.manual({http: ‘HOST:PORT’}));
let driver = new webdriver.Builder()
.forBrowser(‘chrome’)
.setChromeOptions(opts)
.build();
try {
await driver.get(“https://selenium.dev”);
}
finally {
await driver.quit();
}
}());
{{< / code-panel >}}
{{< code-panel language=“kotlin” >}}
import org.openqa.selenium.Proxy
import org.openqa.selenium.WebDriver
import org.openqa.selenium.chrome.ChromeDriver
import org.openqa.selenium.chrome.ChromeOptions
class proxyTest {
fun main() {
val proxy = Proxy()
proxy.setHttpProxy("<HOST:PORT>")
val options = ChromeOptions()
options.setCapability("proxy", proxy)
val driver: WebDriver = ChromeDriver(options)
driver["https://www.google.com/"]
driver.manage().window().maximize()
driver.quit()
}
}
{{< / code-panel >}}
{{< / code-tab >}}
官方链接为:https://www.selenium.dev/documentation/en/webdriver/http_proxies