Selenium?有很多功能,?但其核心是?web?瀏覽器自動(dòng)化的一個(gè)工具集,它允許用戶模擬終端用戶執(zhí)行的常見活動(dòng);將文本輸入到字段中,選擇下拉值和復(fù)選框,并單擊文檔中的鏈接。?它還提供許多其他控件,比如鼠標(biāo)移動(dòng)、任意?JavaScript?執(zhí)行等等。
雖然?Selenium?主要用于網(wǎng)站的前端測(cè)試,但其核心是瀏覽器用戶代理庫(kù)。本次來(lái)說(shuō)說(shuō),Python使用Selenium調(diào)用Chrome瀏覽器并通過(guò)HTTP代理進(jìn)行自動(dòng)化測(cè)試:
白名單模式代碼示例:
```python
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
targetURL = "http://myip.ipip.net" #訪問(wèn)的目標(biāo)站點(diǎn)
proxyAddr = "您的代理IP:端口號(hào)"
if __name__ == '__main__':
browser_location = r".\Chrome\chrome.exe" #指定瀏覽器路徑位置
driver_location = r".\Chrome\chromedriver.exe" #指定Driver路徑位置
option = webdriver.ChromeOptions()
option.binary_location = browser_location #設(shè)置瀏覽器位置
option.add_argument("--start-maximized") #窗口最大化運(yùn)行
option.add_argument('--proxy-server=%(server)s' % {"server": proxyAddr})
driver = webdriver.Chrome(service=Service(driver_location), options=option)
driver.get(targetURL)
print(driver.page_source)
```
運(yùn)行結(jié)果:
賬密模式代碼如下:
?
from?selenium?import?webdriver
from?selenium.webdriver.chrome.service?import?Service
import?string
import?zipfile
targetURL?=?"http://d.qg.net/ip"?#訪問(wèn)的目標(biāo)站點(diǎn)
proxyHost?=?"您的代理IP"
proxyPort?=?"端口號(hào)"?
authKey?=?"請(qǐng)改成您的Key"?
password?=?"請(qǐng)改成您的AuthPwd"
#?賬密模式
def?create_proxy_auth_extension(proxy_host,?proxy_port,?proxy_username,?proxy_password,?scheme='http',?plugin_path=None):
????if?plugin_path?is?None:
????????plugin_path?=?r'./{}_{}_qgnet_proxyauth_plugin.zip'.format(proxy_username,?proxy_password)
????manifest_json?=?"""
????????{
????????????"version":?"1.0.0",
????????????"manifest_version":?2,
????????????"name":?"QG.NET?Proxy",
????????????"permissions":?[
????????????????"proxy",
????????????????"tabs",
????????????????"unlimitedStorage",
????????????????"storage",
????????????????"",
????????????????"webRequest",
????????????????"webRequestBlocking"
????????????],
????????????"background":?{
????????????????"scripts":?["background.js"]
????????????},
????????????"minimum_chrome_version":"22.0.0"
????????}
????????"""
????background_js?=?string.Template(
????????"""
????????var?config?=?{
????????????mode:?"fixed_servers",
????????????rules:?{
????????????????singleProxy:?{
????????????????????scheme:?"${scheme}",
????????????????????host:?"${host}",
????????????????????port:?parseInt(${port})
????????????????},
????????????????bypassList:?["localhost"]
????????????}
??????????};
????????chrome.proxy.settings.set({value:?config,?scope:?"regular"},?function()?{});
????????function?callbackFn(details)?{
????????????return?{
????????????????authCredentials:?{
????????????????????username:?"${username}",
????????????????????password:?"${password}"
????????????????}
????????????};
????????}
????????chrome.webRequest.onAuthRequired.addListener(
????????????callbackFn,
????????????{urls:?[""]},
????????????['blocking']
????????);
????????"""
????).substitute(
????????host=proxy_host,
????????port=proxy_port,
????????username=proxy_username,
????????password=proxy_password,
????????scheme=scheme,
????)
????with?zipfile.ZipFile(plugin_path,?'w')?as?zp:
????????zp.writestr("manifest.json",?manifest_json)
????????zp.writestr("background.js",?background_js)
????return?plugin_path
if?__name__?==?'__main__':
????#?browser_location?=?r"C:\Users\Administrator\Desktop\Chrome\chrome.exe"??#?指定瀏覽器路徑位置
????driver_location?=?r"C:\Users\Administrator\Desktop\Chrome\chromedriver.exe"??#?指定Driver路徑位置
????proxy_auth_plugin_path?=?create_proxy_auth_extension(
????????proxy_host=proxyHost,
????????proxy_port=proxyPort,
????????proxy_username=authKey,
????????proxy_password=password)
????option?=?webdriver.ChromeOptions()
????#?option.binary_location?=?browser_location?#設(shè)置瀏覽器位置
????option.add_argument("--start-maximized")?#窗口最大化運(yùn)行
????option.add_extension(proxy_auth_plugin_path)?#添加proxy插件
????driver?=?webdriver.Chrome(service=Service(driver_location),?options=option)
????driver.get(targetURL)
????print(driver.page_source)
?
返回結(jié)果如下:文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-738561.html
文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-738561.html
到了這里,關(guān)于新手教程 | Python自動(dòng)化測(cè)試Selenium+chrome連接HTTP代理(賬密+白名單)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!