在 Chrome 中使用 selenium wire 更改用户代理

Change user agent using selenium wire in Chrome

我正在尝试更改我的用户代理并将更改后的用户代理打印到终端以检查它是否已成功更改,但是我没有运气。

我正在使用 selenium wire 并尝试对其进行更改,以便我可以登录该网站的移动版本。由于安全原因,我无法放入我想要的用户代理,但是我已经用了好几天了,但运气不好。

请看下面我的代码

driver = webdriver.Chrome('/Users/callum/Desktop/chromedriver')

def interceptor(request):

del request.headers['User-Agent']

request.headers['User-Agent'] = '####'  

driver.get("https://www.google.com")

我也无法从 selenium wire 打印用户代理,我只能使用这种方法。

agent = driver.execute_script("return navigator.userAgent")

print(agent)

有人可以帮忙吗,将不胜感激:)

查看 Chrome 驱动程序的移动仿真功能:

https://chromedriver.chromium.org/mobile-emulation

from seleniumwire import webdriver  # Import from seleniumwire

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"')
browser = webdriver.Chrome(chrome_options=chrome_options)
user_agent = browser.execute_script("return navigator.userAgent;")
print(str(user_agent))
# Go to the Google home page
browser.get('https://www.google.com')

will also work here. For the printing of user-agent string see 中提到的相同 Chrome 选项。