Python 在 Selenium 中更改 Google Chrome 用户代理的方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49565042/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 19:08:29  来源:igfitidea点击:

Way to change Google Chrome user agent in Selenium?

pythongoogle-chromeseleniumselenium-webdriveruser-agent

提问by theCrabNebula

I'm trying to figure out a way whereby whenever I open up Chrome via Selenium (in Python) in this particular script, the Chrome page automatically opens up with another user agent selected - in this case, Microsoft Edge Mobile (but I will be accessing it from the desktop).

我试图找出一种方法,每当我在这个特定脚本中通过 Selenium(在 Python 中)打开 Chrome 时,Chrome 页面会自动打开并选择另一个用户代理 - 在这种情况下,Microsoft Edge Mobile(但我会从桌面访问它)。

So, after doing some research, I've been able to piece together the following code, which I thought would execute a user-agent switch in Chrome and then open up a new Bing.com page:

因此,在做了一些研究之后,我已经能够拼凑出以下代码,我认为这些代码会在 Chrome 中执行用户代理切换,然后打开一个新的 Bing.com 页面:

from selenium import webdriver 
from selenium.webdriver.chrome.options

import Options opts = Options()
opts.add_argument("user-agent=Mozilla/5.0 (Windows Phone 10.0; Android 4.2.1; Microsoft; Lumia 640 XL LTE) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Mobile Safari/537.36 Edge/12.10166")
driver = webdriver.Chrome(chrome_options=opts)
driver = webdriver.Chrome("D:\_")
driver.get("https://www.bing.com/")

However, the code doesn't seem to be working and stops before opening up the designated webpage. I'm fairly certain the first half of code is off, but I'm not quite sure how. Any and all help would be deeply appreciated.

但是,该代码似乎不起作用并在打开指定网页之前停止。我相当确定代码的前半部分已关闭,但我不太确定如何关闭。任何和所有帮助将不胜感激。

回答by DebanjanB

A simple way to use a random User Agentwould be using Python's fake_useragentmodule as follows :

使用随机用户代理的一种简单方法是使用 Python 的fake_useragent模块,如下所示:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from fake_useragent import UserAgent

options = Options()
ua = UserAgent()
userAgent = ua.random
print(userAgent)
options.add_argument(f'user-agent={userAgent}')
driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\WebDrivers\ChromeDriver\chromedriver_win32\chromedriver.exe')
driver.get("https://www.google.co.in")
driver.quit()

Result of 3 consecutive execution is as follows :

连续3次执行结果如下:

  1. First Execution :

    Mozilla/5.0 (Windows NT 4.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2049.0 Safari/537.36
    
  2. Second Execution :

    Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.517 Safari/537.36
    
  3. Third Execution :

    Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1309.0 Safari/537.17
    
  1. 第一次执行:

    Mozilla/5.0 (Windows NT 4.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2049.0 Safari/537.36
    
  2. 第二次执行:

    Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.517 Safari/537.36
    
  3. 第三次执行:

    Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1309.0 Safari/537.17
    

回答by Tim Woocker

You should use ChromeOptions from selenium.webdriver:

您应该使用 selenium.webdriver 中的 ChromeOptions:

from selenium import webdriver

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--user-agent="Mozilla/5.0 (Windows Phone 10.0; Android 4.2.1; Microsoft; Lumia 640 XL LTE) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Mobile Safari/537.36 Edge/12.10166"')
driver = webdriver.Chrome(chrome_options=chrome_options)

This should work.

这应该有效。