[英]Change Proxy at runtime in Selenium.Webdriver + PhantomJS using C#
There is a way to dinamically change proxy in PhantomJS at runtime. 有一种方法可以在运行时动态更改PhantomJS中的代理。 Here's the
python
code: 这是
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' : [] })
In C#
I'm trying: 在
C#
我正在尝试:
((IJavaScriptExecutor)driver).ExecuteScript(@"phantom.setProxy(""10.0.0.1"", 80)");
Getting exception: 获取异常:
{"errorMessage":"Can't find variable: phantom","request":{"headers":{"Accept":"application/json, image/png","Connection":"Close","Content-Length":"62","Content-Type":"application/json;charset=utf-8","Host":"localhost:57378"},"httpVersion":"1.1","method":"POST","post":"{\\"script\\":\\"phantom.setProxy(\\\\"10.0.0.1\\\\", 80)\\",\\"args\\":[]}","url":"/execute","urlParsed":{"anchor":"","query":"","file":"execute","directory":"/","path":"/execute","relative":"/execute","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/execute","queryKey":{},"chunks":["execute"]},"urlOriginal":"/session/513f2130-26b4-11e7-b459-45c0e08b428c/execute"}}
{“ errorMessage”:“找不到变量:phantom”,“ request”:{“ headers”:{“ Accept”:“ application / json,image / png”,“ Connection”:“ Close”,“ Content-长度”:“ 62”,“内容类型”:“ application / json; charset = utf-8”,“主机”:“ localhost:57378”},“ httpVersion”:“ 1.1”,“方法”:“ POST “,” post“:” {\\“ script \\”:\\“ phantom.setProxy(\\\\” 10.0.0.1 \\\\“,80)\\”,\\“ args \\”:[]}“,” url“: “ / execute”,“ urlParsed”:{“ anchor”:“”,“ query”:“”,“ file”:“ execute”,“ directory”:“ /”,“ path”:“ / execute”,“ relative“:” / execute“,” port“:”“,” host“:”“,” password“:”“,” user“:”“,” userInfo“:”“,” authority“:”“, “ protocol”:“”,“ source”:“ / execute”,“ queryKey”:{},“ chunks”:[“ execute”]},“ urlOriginal”:“ / session / 513f2130-26b4-11e7-b459- 45c0e08b428c / execute“}}
One limitation in .NET is that you won't be able to do this remotely (via Selenium Grid or similar); .NET中的一个限制是您将无法远程执行此操作(通过Selenium Grid或类似方法)。 you would only be able to do this locally.
您将只能在本地执行此操作。 The code to do so would look something like the following:
这样做的代码如下所示:
// 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);");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.