我正在尝试使用 selenium 和 phantomjs 单击在新选项卡 (target="_blank") 中打开的链接,但问题是当 phantomjs 打开链接时,它没有设置为其定义的用户代理使用 DesiredCapabilities。如果链接在当前窗口中打开,一切正常,但一旦在新窗口中打开,用户代理就是默认的!
如何全局设置用户代理? (顺便说一句,我正在使用 python)
最佳答案
经过一番调查,我发现问题似乎出自ghost driver。所以我检查了 ghost 驱动程序 github,发现问题是从 2013 年开始存在的,并且有一个临时解决方案。我在这里写了解决方案,因为它不是谷歌搜索中 github 问题的结果 (https://github.com/detro/ghostdriver/issues/273):
Guaranteed solution for links target = "_blank" and window.open - set phantomjs.page.customHeaders.User-Agent in desiredCapabilities. In my case (window.open) may open a new window with the "about:blank" url, then find it in the handles array to activate and surf.
因此,虽然不建议在 selenium 文档的自定义 header 中设置 userAgent,但这是目前全局设置 userAgent 的唯一方法,因此它不会在新窗口中更改。
关于selenium - PhantomJS + Selenium,如何为在新窗口中打开的链接设置用户代理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31356806/