問題描述
我想向 python 客戶端.
最初回答于 2018 年 11 月 6 日 8:00
否.當您使用 ChromeOptions 配置 ChromeDriver 實例以啟動新的 Chrome 瀏覽器會話 時,ChromeDriver 的配置在 ChromeDriver 的整個生命周期中保持不變并且保持不可編輯.因此,當 WebDriver 實例執行循環發出 10 個請求時,您無法更改 用戶代理.
即使您能夠提取 ChromeDriver 和 ChromeSession 屬性,例如UserAgent、Session ID、Cookies 和已啟動的瀏覽會話中的其他會話屬性,您仍然不會能夠更改 ChromeDriver 的那些屬性.
更簡潔的方法是在 tearDown(){}
方法中調用 driver.quit()
以 close 和 destroy 優雅地 ChromeDriver 和 Chrome Browser 實例,然后跨越一組新的 ChromeDriver 和 Chrome 瀏覽器 具有新配置集的實例.
您可以在此處找到有關 如何用 selenium 重新連接到 webdriver 打開的瀏覽器?
參考
您可以在以下位置找到一些相關的詳細討論:
- 如何更改用戶使用 Selenium 和 Python 的代理
- Selenium webdriver:修改導航器.webdriver 標志以防止硒檢測
I want to make 10 requests to https://www.google.com/ but with random user agents using selenium and python. I've a loop and inside that loop I'm making 10 requests with random user agents (using fake-user agent). The main problem is for every request web driver is opening a new instance of google chrome and I want to do this in one single instance but with different user agents. How can I make this possible ? 1 google chrome instance and 10 requests with 10 random user agents. Here is my code:
chrome_options = Options()
chrome_options.add_argument('no-sandbox')
chrome_options.add_argument("--start-maximized")
ua = UserAgent()
for i in range(0, 10):
userAgent = ua.random
chrome_options.add_argument('--user-agent="' + str(userAgent) + '"')
driver1 = webdriver.Chrome(chrome_options=chrome_options,
executable_path="C:/Python34/chromedriver")
driver1.get('https://www.google.com/')
time.sleep(5)
First the update 1
execute_cdp_cmd(): With the availability of execute_cdp_cmd(cmd, cmd_args)
command now you can easily execute google-chrome-devtools commands using Selenium. Using this feature you can modify the user-agent easily to prevent Selenium from getting detected.
Code Block:
from selenium import webdriver driver = webdriver.Chrome(executable_path=r'C:WebDriverschromedriver.exe') print(driver.execute_script("return navigator.userAgent;")) # Setting user agent as Chrome/83.0.4103.97 driver.execute_cdp_cmd('Network.setUserAgentOverride', {"userAgent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36'}) print(driver.execute_script("return navigator.userAgent;")) # Setting user agent as Chrome/83.0.4103.53 driver.execute_cdp_cmd('Network.setUserAgentOverride', {"userAgent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.53 Safari/537.36'}) print(driver.execute_script("return navigator.userAgent;")) driver.get('https://www.httpbin.org/headers')
Console Output:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36 Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36 Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.53 Safari/537.36
Browser Snapshot:
Legend: 1 - Applicable only to Selenium python clients.
Originally answered Nov 6 '18 at 8:00
No. When you configure an instance of a ChromeDriver with ChromeOptions to initiate a new Chrome Browser Session the configuration of the ChromeDriver remains unchanged throughout the lifetime of the ChromeDriver and remains uneditable. So you can't change the user agent when the WebDriver instance is executing the loop making 10 requests.
Even if you are able to extract the ChromeDriver and ChromeSession attributes e.g. UserAgent, Session ID, Cookies and other session attributes from the already initiated Browsing Session still you won't be able to change those attributes of the ChromeDriver.
A cleaner way would be to call driver.quit()
within tearDown(){}
method to close and destroy the ChromeDriver and Chrome Browser instances gracefully and then span a new set of ChromeDriver and Chrome Browser instance with the new set of configurations.
Here you can find a relevant discussion on How can I reconnect to the browser opened by webdriver with selenium?
Reference
You can find a couple of relevant detailed discussions in:
- How to change the User Agent using Selenium and Python
- Selenium webdriver: Modifying navigator.webdriver flag to prevent selenium detection
這篇關于如何在每個請求上使用 selenium python 輪換各種用戶代理的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!