如何在 PhantomDriver(无头浏览器)中没有 findElement 函数错误的情况下隐藏 FirefoxDriver(使用 Selenium)?
How to hide FirefoxDriver (using Selenium) without findElement function error in PhantomDriver(headless browser)?
我尝试制作隐藏的 FirefoxDriver。根据我的研究,我必须使用 PhantomJSDriver,但是当我使用 PhantomJSDriver 时,driver.FindElement 语句不再起作用。
var options = new PhantomJSOptions();
options.AddAdditionalCapability("phantomjs.page.settings.userAgent",
"Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/40.0.2214.94 Safari/537.36");
PhantomJSOptions p = new PhantomJSOptions();
var service = PhantomJSDriverService.CreateDefaultService();
service.SslProtocol = "any";
service.ProxyType = "http";
service.WebSecurity = false;
service.IgnoreSslErrors = true;
var driver = new PhantomJSDriver(service, options);
driver.Navigate().GoToUrl("https://www.google.com.tr/");
Thread.Sleep(5000);
driver.FindElement(By.XPath("//*[@id='lst-ib']")).SendKeys("edd");
string s = driver.Url;
Console.WriteLine(s);
错误信息:
'OpenQA.Selenium.NoSuchElementException' 类型的未处理异常发生在 WebDriver.dll
附加信息:
{"errorMessage":"Unable to find element with xpath '//[@id='_fZl']/span/svg/path'","request":{"headers":{"Accept":"application/json, image/png","Connection":"Close" ,"Content-Length":"57","Content-Type":"application/json;charset=utf-8","Host":"localhost:50454"},"httpVersion":"1.1","method":"POST","post":"{\"using\":\"xpath\",\"value\":\"//[@id= '_fZl']/span/svg/path\"}","url":"/元素","urlParsed":{"anchor":"","query":"", "file":"element","directory":"/","path":"/元素","relative":"/元素","port": "","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/元素","queryKey":{},"chunks":["element"]},"urlOriginal":" =66=]"}}
还有其他方法可以隐藏 FirefoxDriver 吗?
你能帮帮我吗?
无法隐藏 FirefoxDriver 本身。您可以 运行 它在虚拟机上并最小化 vm window 但这对大多数人来说不切实际。
让我们来看看你真正的问题。看起来 Google 正在使用 js 分配搜索框的 ID 以防止抓取,因为这违反了他们的服务条款。
这里有几个选项:
1) 使用名称 'q' 定位元素,因为它的名称与 phantomjs 或 firefox 无关。
2) 直接进入搜索结果页面:https://www.google.com.tr/search?q=edd
我解决了。
首先
我们可以通过以下代码在不显示其控制台的情况下使用 PhantomJS:
IWebDriver driver;
var driverService = PhantomJSDriverService.CreateDefaultService();
driverService.HideCommandPromptWindow = true;
driver = new PhantomJSDriver(driverService);
其次是我提到的错误。
Google return 不同的 HTML 浏览器页面,因此 PhantomJS 浏览器中的 Id 或 Xpath 将与我打开 Firefox 时导出的不同。
当我使用
string html=driver.PageSource;
要了解正确的 XPath 或 Id,findElement 功能运行良好。
例如:对于 Google 站点结果
FirefoxDriver 中第一个 link 的 XPath 是
"//*[@id='rso']/div/div/div[1]/div/div/h3/a"
PhantomJSDriver 中第一个 link 的 XPath 是
"//*[@id='ires']//ol/div[1]/h3/a"
自版本 55+ for Linux 和 56+ for Windows & OSX,Firefox 支持 -headless
command line option. It shall be used like this:
o = selenium.webdriver.FirefoxOptions()
o.set_headless()
driver=selenium.webdriver.Firefox(options=o)
C#中对应代码would be:
var o = new FirefoxOptions()
o.AddArgument('-headless')
var driver = new FirefoxDriver(o)
因为 .NET 包装器 doesn't support the .headless
property.
我尝试制作隐藏的 FirefoxDriver。根据我的研究,我必须使用 PhantomJSDriver,但是当我使用 PhantomJSDriver 时,driver.FindElement 语句不再起作用。
var options = new PhantomJSOptions();
options.AddAdditionalCapability("phantomjs.page.settings.userAgent",
"Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/40.0.2214.94 Safari/537.36");
PhantomJSOptions p = new PhantomJSOptions();
var service = PhantomJSDriverService.CreateDefaultService();
service.SslProtocol = "any";
service.ProxyType = "http";
service.WebSecurity = false;
service.IgnoreSslErrors = true;
var driver = new PhantomJSDriver(service, options);
driver.Navigate().GoToUrl("https://www.google.com.tr/");
Thread.Sleep(5000);
driver.FindElement(By.XPath("//*[@id='lst-ib']")).SendKeys("edd");
string s = driver.Url;
Console.WriteLine(s);
错误信息:
'OpenQA.Selenium.NoSuchElementException' 类型的未处理异常发生在 WebDriver.dll
附加信息: {"errorMessage":"Unable to find element with xpath '//[@id='_fZl']/span/svg/path'","request":{"headers":{"Accept":"application/json, image/png","Connection":"Close" ,"Content-Length":"57","Content-Type":"application/json;charset=utf-8","Host":"localhost:50454"},"httpVersion":"1.1","method":"POST","post":"{\"using\":\"xpath\",\"value\":\"//[@id= '_fZl']/span/svg/path\"}","url":"/元素","urlParsed":{"anchor":"","query":"", "file":"element","directory":"/","path":"/元素","relative":"/元素","port": "","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/元素","queryKey":{},"chunks":["element"]},"urlOriginal":" =66=]"}}
还有其他方法可以隐藏 FirefoxDriver 吗? 你能帮帮我吗?
无法隐藏 FirefoxDriver 本身。您可以 运行 它在虚拟机上并最小化 vm window 但这对大多数人来说不切实际。
让我们来看看你真正的问题。看起来 Google 正在使用 js 分配搜索框的 ID 以防止抓取,因为这违反了他们的服务条款。
这里有几个选项:
1) 使用名称 'q' 定位元素,因为它的名称与 phantomjs 或 firefox 无关。
2) 直接进入搜索结果页面:https://www.google.com.tr/search?q=edd
我解决了。 首先 我们可以通过以下代码在不显示其控制台的情况下使用 PhantomJS:
IWebDriver driver;
var driverService = PhantomJSDriverService.CreateDefaultService();
driverService.HideCommandPromptWindow = true;
driver = new PhantomJSDriver(driverService);
其次是我提到的错误。 Google return 不同的 HTML 浏览器页面,因此 PhantomJS 浏览器中的 Id 或 Xpath 将与我打开 Firefox 时导出的不同。 当我使用
string html=driver.PageSource;
要了解正确的 XPath 或 Id,findElement 功能运行良好。
例如:对于 Google 站点结果 FirefoxDriver 中第一个 link 的 XPath 是
"//*[@id='rso']/div/div/div[1]/div/div/h3/a"
PhantomJSDriver 中第一个 link 的 XPath 是
"//*[@id='ires']//ol/div[1]/h3/a"
自版本 55+ for Linux 和 56+ for Windows & OSX,Firefox 支持 -headless
command line option. It shall be used like this:
o = selenium.webdriver.FirefoxOptions()
o.set_headless()
driver=selenium.webdriver.Firefox(options=o)
C#中对应代码would be:
var o = new FirefoxOptions()
o.AddArgument('-headless')
var driver = new FirefoxDriver(o)
因为 .NET 包装器 doesn't support the .headless
property.