给selenium自定义user-agent

[scrapy] 2021-10-26 圈点139

摘要:给selenium自定义user-agent。分别给firefox driver和chromedriver自定义user-agent的示例方法

给selenium自定义user-agent。分别给firefox driver和chromedriver自定义user-agent的示例方法:


示例1:

from selenium import webdriver

profile = webdriver.FirefoxProfile()

profile.set_preference("general.useragent.override", "whatever you want")

driver = webdriver.Firefox(profile)


示例2:

from selenium import webdriver

from selenium.webdriver.chrome.options import Options

opts = Options()

opts.add_argument("user-agent=whatever you want")

driver = webdriver.Chrome(chrome_options=opts)


检测显示当前user-agent

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


selenium  useragent  

感谢反馈,已提交成功,审核后即会显示