Selenium 2-为IE和Chrome设置用户代理 [英] Selenium 2 - Setting user agent for IE and Chrome

查看:150
本文介绍了Selenium 2-为IE和Chrome设置用户代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于某些测试,我需要在IE和Chrome中更改用户代理值.我遇到的唯一的硒2示例仅适用于FirefoxDriver.

I need to change the user agent value in IE and Chrome for some of our tests. The only selenium 2 examples I've come across only work with FirefoxDriver.

有人能更改IE和Chrome的用户代理吗?

Has anyone managed to change the user agent for IE and Chrome?

标记

推荐答案

我知道这已经很老了,但是几秒钟前偶然发现了它,而且我也找到了真正的解决方案(至少对于最新版本而言)硒.

I know this is veery old by now, but I stumbled across it and a few seconds ago and I also found the real solution (at least for the latest version of Selenium).

所以我们开始(Python,伪造iPad UA的示例):

So here we go (Python, example faking the iPad UA):

from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_argument('--user-agent=Mozilla/5.0 (iPad; CPU OS 5_0 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A334 Safari/7534.48.3')

driver = webdriver.Chrome(chrome_options=options)

# ...loads of fun...

我希望这对其他有相同问题的人有所帮助.哦,它还可以与所有其他Chrome命令行选项一起使用. Njoy;)

I hope this is helpful for anyone else having the same problem. Oh, and it also works with all the other Chrome command line options. Njoy ;)

这篇关于Selenium 2-为IE和Chrome设置用户代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆