微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何成功更改用Python编写的Selenium脚本中的User-Agent?

如何解决如何成功更改用Python编写的Selenium脚本中的User-Agent?

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

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和Mobile执行搜索(Bing可以跟踪您搜索的位置)。但是,当我使用脚本时,它无法识别搜索是通过Edge&mobile进行的。

该脚本曾经可以使用,但是最近Bing无法识别已更改的User-Agent,即使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都是最新的。当我使用脚本时(我的用户界面似乎更改为移动设备),该界面会打开并运行搜索,但是无论出于何种原因,Bing都不会将此搜索注册为移动设备搜索或Edge搜索。但是,当我手动打开Chrome并手动更改User-Agent时,它确实可以工作。我有一个脚本,它在没有User-Agent代码的情况下也可以执行相同的操作(因此,只需通过常规的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 Mobile用户代理进行搜索,考虑到在测试自动化中Chrome UI更改为移动用户,这似乎是在做,但是Bing不能这样识别。有什么想法吗?

解决方法

将此添加到您的代码中:

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"})

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。