【问题标题】:How to successfully change User-Agent in Selenium script written in Python?如何成功更改用 Python 编写的 Selenium 脚本中的 User-Agent?
【发布时间】:2020-12-15 14:15:53
【问题描述】:

我目前正在使用 Selenium 通过 Chrome 自动执行 Bing 搜索。我希望脚本自动打开 Chrome,并将用户代理设置为 Edge Mobile。根据 Chrome devtools 中的信息,User-Agent 应为:

Mozilla/5.0 (Linux; Android 8.1.0; Pixel Build/OPM4.171019.021.D1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.109 Mobile Safari/537.36 EdgA/42.0.0.2057

当我尝试在定期打开的 Chrome 上通过 Bing 搜索时,我手动将 User-Agent 更改为 Edge 和 Mobile,Bing 承认我正在通过 Edge 和移动执行搜索(Bing 可以跟踪您搜索的位置)。但是,当我使用我的脚本时,它无法识别搜索是通过 Edge 和移动端进行的。

这个脚本过去可以工作,但最近 Bing 无法识别更改的用户代理,即使 UI 似乎发生了变化。这是我的完整代码:

import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from fake_useragent import UserAgent
from selenium.webdriver.common.keys import Keys

options = Options()
options.add_argument("start-maximized")
options.add_argument("disable-infobars")
options.add_argument("--disable-extensions")
ua = UserAgent()
userAgent = ua.random
print(userAgent)
options.add_argument(f'user-agent={"Mozilla/5.0 (Linux; Android 8.1.0; Pixel Build/OPM4.171019.021.D1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.109 Mobile Safari/537.36 EdgA/42.0.0.2057"}')
driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\_Coding\Selenium\chromedriver.exe')

driver.get('https://login.live.com/login.srf?wa=wsignin1.0&rpsnv=13&ct=1522552641&rver=6.7.6631.0&wp=MBI&wreply=https%3a%2f%2fwww.bing.com%2fsecure%2fPassport.aspx%3frequrl%3dhttps%253a%252f%252fwww.bing.com%252f%253fwlexpsignin%253d1&lc=1033&id=264960&pcexp=false&CSRFToken=94467ae5-f34c-42a8-be9c-964caff9ac54&aadredir=1')
print("Page Title is : %s" %driver.title)
element = driver.find_element_by_xpath("//input[@class='form-control ltr_override input ext-input text-box ext-text-box' and @name='loginfmt']")
element.click()
element.clear()
element.send_keys("example@gmail.com")

time.sleep(2)

driver.find_element_by_id('idSIButton9').send_keys("\n")

time.sleep(1)

password = driver.find_element_by_xpath("//input[@class='form-control input ext-input text-box ext-text-box' and @name='passwd']")
password.click()
password.clear()
password.send_keys("password")

time.sleep(2)

driver.find_element_by_id('idSIButton9').send_keys("\n")

time.sleep(2)

element = driver.find_element_by_id("sb_form_q")
element.send_keys("BLUE")
element.send_keys(Keys.RETURN)

time.sleep(2)

element = driver.find_element_by_name("q")

element.clear()

element.send_keys("FINISH")
element.send_keys(Keys.RETURN)

time.sleep(1)

driver.quit()

我相信我所有的软件包和 chromedriver 都是最新的。当我使用脚本时,界面会打开并运行搜索(并且 UI 似乎更改为移动设备),但无论出于何种原因,必应不会将此搜索注册为移动搜索或 Edge 搜索。但是当我手动打开 Chrome 并手动更改用户代理时它确实有效。我有另一个脚本可以在没有用户代理代码的情况下执行相同的操作(因此只需通过常规 Chrome 桌面用户代理进行搜索),Bing 会正确识别这一点(作为桌面搜索)。

这是我在运行上述脚本时在命令提示符中收到的错误代码:

DevTools listening on ws://127.0.0.1:65529/devtools/browser/02af2e70-b10a-4d85-9d5d-01e4e828a911
Page Title is : Sign in to Bing
[17928:16612:0826/140206.701:ERROR:device_event_log_impl.cc(208)] [14:02:06.701] Bluetooth: bluetooth_adapter_winrt.cc:1074 Getting Default Adapter failed.

我只是希望脚本使用 Edge 和 Android 移动用户代理进行搜索,考虑到 Chrome UI 在测试自动化中更改为移动用户代理,它似乎正在执行此操作,但 Bing 无法识别它。有什么想法吗?

【问题讨论】:

    标签: python selenium google-chrome microsoft-edge user-agent


    【解决方案1】:

    将此添加到您的代码中:

    driver.execute_cdp_cmd('Network.setUserAgentOverride', {"userAgent":"Mozilla/5.0 (Linux; Android 8.1.0; Pixel Build/OPM4.171019.021.D1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.109 Mobile Safari/537.36 EdgA/42.0.0.2057", "platform":"Windows"})
    

    【讨论】:

    • 感谢您的回复!你会在哪里添加该代码?我已经在几个地方尝试过,脚本甚至都不会运行:(
    • 抱歉回复晚了,只需从您的代码中删除此代码:from fake_useragent import UserAgentua = UserAgent()userAgent = ua.random, print(userAgent),然后运行,如果它不更改 UserAgent,请将我的代码放在此代码下driver = webdriver.Chrome(chrome_options=options, executable_path=r'
    • 再次感谢。我尝试了您的建议,首先删除您提到的代码并运行脚本,然后添加您的代码并运行它。从技术上讲,所有变体都使用看起来像移动设备的 UI 来启动和运行自动化,它们只是不像我原始帖子中的代码那样在 Bing 端注册为 Edge/Mobile 搜索。几周前我的原始代码停止工作 - 也许 Edge 或 Chrome 更新是罪魁祸首?
    猜你喜欢
    • 2016-08-05
    • 2017-08-19
    • 2015-04-26
    • 1970-01-01
    • 1970-01-01
    • 2010-09-14
    • 2011-05-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多