java - Selenium WebDriver - Java - 更改用户代理
问题描述
我目前正在 selenium wbesite 上浏览 selenium webdriver。
网站的一部分讨论了“更改用户代理”并解释了如何做到这一点:https ://docs.seleniumhq.org/docs/03_webdriver.jsp#chapter03-reference但是,没有“真实”或“模拟”示例在“动作”中显示了这一点。
所以,我想知道这里是否有人可能拥有或能够共享包含该项目的代码片段,显示这个“在行动中”?一个模拟项目,您可能拥有或设计自己来实践这一点,就足够了。
我已经在网上做了很多研究,试图找到一个这样的例子,但没有成功。
解决方案
从您提供的链接中,您可以User-Agent
使用以下代码更改:
火狐:
FirefoxProfile profile = new FirefoxProfile();
profile.addAdditionalPreference("general.useragent.override", "user-agent-string");
WebDriver driver = new FirefoxDriver(profile);
铬合金:
ChromeOptions chrome = new ChromeOptions();
chrome.addArguments("user-agent=YOUR_USER_AGENT");
但Java
你可以轻松切换到任何支持 selenium 的语言,因为它们都实现FirefoxProfile
和ChromeOptions
推荐阅读
- python - 如何在美汤中获得超过8个项目
- python - 替换列值的替代选项
- python - 尝试使用 Selenium 发布图像/视频的问题
- python - 用scrapy刮了两个月后得到403
- python - 如何使用 python 和 pandas read_fwf 函数处理位于 Azure blob 存储中的文件
- visual-studio - Web 编译器在 wwwroot 之外看不到文件
- xml - XQuery 3:计算文档中元素名称的出现次数
- firebase-storage - FirebaseUI 存储在没有 jcenter() 的情况下无法工作
- matlab - 子集一个字符串
- php - Laravel 8.4 API GET Routes 返回 404