简体   繁体   English

使用C#在Selenium.Webdriver + PhantomJS中在运行时更改代理

[英]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.

相关问题 使用phantomjs驱动程序C#在Selenium中进行代理 - Proxy in Selenium using phantomjs Driver C# Selenium.WebDriver! 如何使用C#获取下拉菜单的子菜单? - Selenium.WebDriver! How get submenu of dropdown menu using C#? C# selenium/phantomjs 设置全局代理 - C# selenium/phantomjs set global proxy Selenium.WebDriver 2.32.1 C#-等待页面加载后隐藏LOADING DIV - Selenium.WebDriver 2.32.1 C# - Wait untill LOADING DIV is hidden after Page Load Selenium.WebDriver 3.0.1与netcoreapp1.1不兼容 - C#,VS 2017 Community for Mac - Selenium.WebDriver 3.0.1 is not compatible with netcoreapp1.1 - C#, VS 2017 Community for Mac C#,Atata 框架:将 Selenium.WebDriver 更新到 v 4.3 后无法使用 .Hover() 控制方法 - C#, Atata Framework: Unable to use .Hover() Control method after updated Selenium.WebDriver to v 4.3 C# Selenium WebDriver FireFox Profile - 使用代理和身份验证 - C# Selenium WebDriver FireFox Profile - using proxy with Authentication Selenium Webdriver PhantomJS C#总是打开一个cmd窗口 - Selenium Webdriver PhantomJS C# always opens a cmd window C#使用PhantomJS webdriver ExecutePhantomJS过滤掉图像的示例 - C# example of using PhantomJS webdriver ExecutePhantomJS to filter out images Selenium C#PhantomJs无法更改视口高度 - Selenium C# PhantomJs can't change viewport height
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM