selenium设定请求头

如果只是需要设定User-Agent可以用如下所示方式
dcap = dict(DesiredCapabilities.PHANTOMJS)
dcap["phantomjs.page.settings.userAgent"] = (
"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.103 Safari/537.36" )

browser = webdriver.PhantomJS(desired_capabilities=dcap)

如果是全请求头设定的话(此处是转载自他人博客内容转载地址如下http://www.cnblogs.com/zhao-ying-jie/p/7082723.html
headers = { 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'Accept-Language': 'zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3', 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/603.2.4 (KHTML, like Gecko) Version/10.1.1 Safari/603.2.4', 'Connection': 'keep-alive' }
cap = DesiredCapabilities.PHANTOMJS.copy()//使用copy()防止修改原代码定义dict
for key, value in headers.items(): cap['phantomjs.page.customHeaders.{}'.format( key)] = value
browser = webdriver.PhantomJS(desired_capabilities=cap)

转载于:https://www.cnblogs.com/crawer-1/p/7634318.html

你可能感兴趣的:(python,java,测试)