python - 对 Selenium 使用随机 userAgent(python)

标签 python selenium automated-tests user-agent selenium-firefoxdriver

需要使用随机的userAgent。我的代码是:

#!/usr/bin/python
# Import selenium
from selenium import webdriver

# init Profile options for navigation
fp = webdriver.FirefoxProfile()

# Set userAgent
fp.set_preference("general.useragent.override", "custom userAgent")
fp.update_preferences()

最佳答案

我个人使用 fake-useragent图书馆。

您可以生成随机用户代理:

from fake_useragent import UserAgent
from selenium import webdriver

fp = webdriver.FirefoxProfile()

fp.set_preference("general.useragent.override", UserAgent().random)
fp.update_preferences()

关于python - 对 Selenium 使用随机 userAgent(python),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42620282/

相关文章:

python - 删除时django-dynamic-formset问题

python - 在 Django 中访问同一数据库中的其他表

python - 没有 'consistent method resolution'错误的两级抽象类层次结构

java - 用于定位 iram 并切换到它的循环不起作用

Selenium 网格 : Identify WebDriver node where a test failure occured

Python从字符串中删除空格将其转换为int

google-chrome - Puppeteer 不是容器中的 headless Chrome

android - 在android appium测试中模拟输入/发送键

c# - Selenium 在文本框中输入无效文本(半文本)

python - 接收异步数据源以使用 Chaco 进行绘图的可行方法是什么?