如何隐藏FirefoxDriver(使用Selenium)没有FindElment函数错误(无头浏览器)?[英] How to hide FirefoxDriver (using Selenium) without findElement function error in PhantomDriver(headless browser)?

问题描述

我试图制作隐藏的firefoxdriver.根据我的研究,我必须使用PhantomJsdriver,但是当我使用Phantomjsdriver驱动程序时.文件说明不再工作.

        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);

错误消息:

Webdriver.dll类型"OpenQA.Selenium.nosuchelementException"类型的异常发生在WebDriver.dll

附加信息: {"errormessage":"无法查找XPath'// [@ id ='_ fzl']/span/svg/path'","请求":{"标题":{"接受":"应用/JSON,Image/PNG","连接":"关闭","Content-Length":"57","Content-Type":"Application/Json; Charset = UTF-8","主机":" localhost:50454"},"httpversion":"1.1","方法":"post","post":"{\"使用\":\"xpath \",\"value \":\"// [@ id ='_ fzl']/span/svg/path \"}","url":"/元素","URLPARSED":{"锚":""","查询":"" ,"文件":"元素","目录":"/","路径":"/元素","相对":"/元素","端口":"端口":"","主机":""",","密码":"",","用户":"","userinfo":""","权限":""",","协议":"","源":"/元素","querykey":{}, "块":["元素"]},"ulloriginal":"/会话/feab13f0-720f-11e7-80b3-452aee308158/component"}}

是否有另一种方法来隐藏FirefoxDriver? 你能帮我吗?

推荐答案

我解决了它. 首先 我们可以使用Phantomjs而不显示本准则的控制台:

IWebDriver driver; 
var driverService = PhantomJSDriverService.CreateDefaultService();
driverService.HideCommandPromptWindow = true;
driver = new PhantomJSDriver(driverService);

第二是我提到的错误. Google返回浏览器的不同HTML页面,因此Phantomjs浏览器中的ID或XPath将与我打开Firefox时的导出. 当我使用

string html=driver.PageSource;

要知道正确的XPath或ID,FindElement Functiom正在运行.

例如:用于Google网站结果 FirefoxDriver中的第一个链接的XPath是

"//*[@id='rso']/div/div/div[1]/div/div/h3/a"

PhantomJsdriver中的第一个链接的XPath是

"//*[@id='ires']//ol/div[1]/h3/a"

其他推荐答案

自版本55+ for linux和56+ for windows&osx,firefox支持 -headless 命令行选项. 它应该像这样使用:

o = selenium.webdriver.FirefoxOptions()
o.set_headless()
driver=selenium.webdriver.Firefox(options=o)

在c#

var o = new FirefoxOptions()
o.AddArgument('-headless')
var driver = new FirefoxDriver(o)

因为.NET包装器不支持.headless属性

其他推荐答案

没有办法隐藏FirefoxDriver本身.您可以在虚拟机上运行它,并最大限度地减少VM窗口,但对于大多数人来说,这不实用.

让我们来看看你的真正问题.它看起来像Google正在为搜索框的ID分配JS以防止刮擦,因为它是针对他们的服务条款.

您在此处有几个选项:

1)使用名称'q'定位元素,因为它的命名为不论phantomjs或firefox如何.

2)直接进入搜索结果页面: https://www. google.com.tr/search?q=edd

本文地址:https://www.itbaoku.cn/post/1739900.html