Selenium 集成教程
Selenium 是最流行的 Web 自动化测试框架。本教程展示如何在 Selenium 中使用 OkkProxy 代理。
Python + Selenium
安装依赖
bash
pip install selenium1
Chrome 配置代理
python
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
# 配置代理
chrome_options = Options()
proxy = "proxy.okkproxy.com:8080"
chrome_options.add_argument(f'--proxy-server=http://{proxy}')
# 创建驱动
driver = webdriver.Chrome(options=chrome_options)
# 如果代理需要认证
driver.get('https://example.com')1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
使用认证代理
python
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium_wire import webdriver # 需要安装 selenium-wire
# 配置带认证的代理
options = {
'proxy': {
'http': 'http://username:[email protected]:8080',
'https': 'http://username:[email protected]:8080',
}
}
driver = webdriver.Chrome(seleniumwire_options=options)
driver.get('https://httpbin.org/ip')
print(driver.page_source)
driver.quit()1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Java + Selenium
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 ProxyExample {
public static void main(String[] args) {
Proxy proxy = new Proxy();
proxy.setHttpProxy("proxy.okkproxy.com:8080");
proxy.setSslProxy("proxy.okkproxy.com:8080");
ChromeOptions options = new ChromeOptions();
options.setProxy(proxy);
WebDriver driver = new ChromeDriver(options);
driver.get("https://example.com");
driver.quit();
}
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
最佳实践
IP 轮换
python
import random
proxies = [
'http://user:[email protected]:8080',
'http://user:[email protected]:8080',
]
def get_driver_with_proxy():
proxy = random.choice(proxies)
options = {'proxy': {'http': proxy, 'https': proxy}}
return webdriver.Chrome(seleniumwire_options=options)1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
错误处理
python
from selenium.common.exceptions import TimeoutException
try:
driver.get('https://example.com')
except TimeoutException:
print("页面加载超时,可能是代理问题")
driver.quit()1
2
3
4
5
6
7
2
3
4
5
6
7