Selenium 有很多功能, 但其核心是 web 瀏覽器自動化的一個工具集,它允許使用者模擬終端使用者執行的常見活動;將文字輸入到欄位中,選擇下拉值和核取方塊,並單擊文件中的連結。 它還提供許多其他控制元件,比如滑鼠移動、任意 JavaScript 執行等等。
雖然 Selenium 主要用於網站的前端測試,但其核心是瀏覽器使用者代理庫。本次來說說, Python使用Selenium呼叫Chrome瀏覽器並透過HTTP代理進行自動化測試:
程式碼示例:
```python from selenium import webdriver from selenium.webdriver.chrome.service import Service targetURL = "訪問的目標站點 proxyAddr = "您的代理IP:埠號" if __name__ == '__main__': browser_location = r".\Chrome\chrome.exe" #指定瀏覽器路徑位置 driver_location = r".\Chrome\chromedriver.exe" #指定Driver路徑位置 option = webdriver.ChromeOptions() option.binary_location = browser_location #設定瀏覽器位置 option.add_argument("--start-maximized") #視窗最大化執行 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) ```
執行結果:
賬密模式程式碼如下:
from selenium import webdriverfrom selenium.webdriver.chrome.service import Service import string import zipfile targetURL = "訪問的目標站點 proxyHost = "您的代理IP" proxyPort = "埠號" authKey = "請改成您的Key" password = "請改成您的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 #設定瀏覽器位置 option.add_argument("--start-maximized") #視窗最大化執行 option.add_extension(proxy_auth_plugin_path) #新增proxy外掛 driver = webdriver.Chrome(service=Service(driver_location), options=option) driver.get(targetURL) print(driver.page_source)
返回結果如下: