javascript - 在 nodejs 中使用 selenium-webdriver 和 phantomjs 设置用户代理

标签 javascript node.js selenium-webdriver phantomjs user-agent

我需要更改驱动 selenium-webdriver 的 PhantomJS 浏览器的用户代理。

我找到了 methods to change the user agent in C#, Ruby and Java

这是我试过的:

var webdriver = require('selenium-webdriver');
var useragent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0";

var driver = new webdriver.Builder().
    withCapabilities(webdriver.Capabilities.phantomjs("phantomjs.page.settings.userAgent", useragent)).
    build();

Web 服务器上的结果仍然是这样的: Mozilla/5.0(Windows NT 6.2;WOW64)AppleWebKit/534.34(KHTML,如 Gecko)PhantomJS/1.9.7 Safari/534.34

最佳答案

    var driver = new webdriver.Builder()
       .withCapabilities(webdriver.Capabilities.phantomjs()
       .set("phantomjs.page.settings.userAgent", useragent))
       .build();

这是一个对我有用的解决方案。

关于javascript - 在 nodejs 中使用 selenium-webdriver 和 phantomjs 设置用户代理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27713911/

相关文章:

javascript - 使用 jquery 显示 div 时将浏览器保持在页面底部

javascript - 为实时流表添加过滤、搜索和分页

node.js - React.js 渲染完成后运行客户端 JS

javascript - 在 Node JS 中发送后无法设置 Header

symfony - Selenium 与 symfony2

selenium - Nutch Selenium 无法连接到 Firefox

java - Selenium : How to solve org. openqa.selenium.InvalidElementStateException: 无效元素状态

javascript - 奇怪的问题:jQuery AJAX 成功数据仅解析最后一个 JSON 节点

javascript - 我如何 Jest 使用 ES6 依赖项

javascript - Google Chrome 中的 "Noisy Tabs"- 检测是否正在播放声音