【问题标题】:Changing Proxy Settings without Closing the Driver in Selenium/Splinter在 Selenium/Splinter 中更改代理设置而不关闭驱动程序
【发布时间】:2018-10-20 12:47:08
【问题描述】:

在旧版本的 Splinter/Selenium this was said not to be possible 中。几年后这个答案声称它is possible with JavaScript,但这段代码对我不起作用(我可能只是未能将它翻译成 Python)。 This answer closes浏览器然后重新打开,我需要窗口/浏览器保持打开状态。

使用 FoxyProxy 之类的插件,动态更改代理非常容易,但我不认为 Selenium 可以与插件交互,因为它们是页面元素?

由于 Splinter 被设计为 Selenium 的一个不太冗长的包装器,如果有一种简单的方法可以实现这一点,那就太棒了。话虽如此,任何仅具有此功能的黑客都会受到赞赏。

【问题讨论】:

  • 你使用的翻译版本是什么?请发布该代码
  • 对我发布的答案有任何反馈吗?

标签: javascript python-3.x selenium splinter


【解决方案1】:

你需要像下面这样使用它

browser.visit("about:config")

script = """
var prefs = Components.classes["@mozilla.org/preferences-service;1"]
.getService(Components.interfaces.nsIPrefBranch);

prefs.setIntPref("network.proxy.type", 1);
prefs.setCharPref("network.proxy.http", "{0}");
prefs.setIntPref("network.proxy.http_port", "{1}");
prefs.setCharPref("network.proxy.ssl", "{0}");
prefs.setIntPref("network.proxy.ssl_port", "{1}");
prefs.setCharPref("network.proxy.ftp", "{0}");
prefs.setIntPref("network.proxy.ftp_port", "{1}");
"""

browser.execute_script(script.format("ProxyIP", "PORT"))

PS:感谢Python Selenium Webdriver - Changing proxy settings on the fly

【讨论】:

猜你喜欢
  • 2015-05-23
  • 2015-07-07
  • 1970-01-01
  • 2020-04-22
  • 2013-11-03
  • 1970-01-01
  • 1970-01-01
  • 2015-06-28
  • 2019-09-22
相关资源
最近更新 更多