有一种方法可以在运行时在PhantomJS中使用dinamically更改代理。这是python
代码:
driver = webdriver.PhantomJS()
driver.command_executor._commands['executePhantomScript'] = ('POST', '/session/$sessionId/phantom/execute')
driver.execute('executePhantomScript', {'script': '''phantom.setProxy("10.0.0.1", 80);''', 'args' : [] })
C#
我正在尝试:
((IJavaScriptExecutor)driver).ExecuteScript(@"phantom.setProxy(""10.0.0.1"", 80)");
获得例外:
{" errorMessage":"无法找到变量: 幻象""请求" {"头" {"接受":"应用/ JSON, 图像/ PNG""连接":"关闭"" Content-Length的":" 62""内容型":"应用/ JSON;字符集= UTF-8""主机":"本地主机:57378"}" httpVersion&# 34;:" 1.1""方法":" POST""后":" {\"脚本\":\" phantom.setProxy(\\" 10.0.0.1 \\&#34 ;, 80)\" \" ARGS \":[]}"" URL":" /执行"&#34 ; urlParsed" {"锚":"""查询":"""文件&#34 ;:"执行""目录":" /""路径":" /执行",& #34;相对于":" /执行""端口":"""主机":"& #34;"密码":"""使用者":""" USERINFO":& #34;""权威":"""协议":"""源&# 34;:" /执行"" queryKey":{},"大块":["执行"]}" urlOriginal":" /会话/ 513f2130-26b4-11e7-b459-45c0e08b428c /执行"}}
答案 0 :(得分:1)
.NET中的一个限制是您无法远程执行此操作(通过Selenium Grid或类似网站);你只能在本地做到这一点。这样做的代码如下所示:
// WARNING! Untested code written without benefit of
// an IDE. Might not run or even compile without modification.
// First, cast the IWebDriver interface back to the concrete
// PhantomJSDriver implementation.
PhantomJSDriver phantomDriver = driver as PhantomJSDriver;
if (phantomDriver != null)
{
// If the cast succeeded, the implementation has the
// ExecutePhantomJS method, which executes script in
// the browser context instead of the page context.
phantomDriver.ExecutePhantomJS("phantom.setProxy('10.0.0.1', 80);");
}