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

查看:132
本文介绍了如何在PhantomDriver(无头浏览器)中隐藏FirefoxDriver(使用Selenium)而没有findElement函数错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试制作隐藏的FirefoxDriver.根据我的研究,我必须使用PhantomJSDriver,但是当我使用PhantomJSDriver驱动程序时,FindElement语句不再起作用.

I try to make hidden FirefoxDriver. According to my research I must use PhantomJSDriver but when I use PhantomJSDriver driver.FindElement statement no longer does not work.

        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'的未处理异常

An unhandled exception of type 'OpenQA.Selenium.NoSuchElementException' occurred in WebDriver.dll

其他信息: {"errorMessage":无法找到带有xpath'// [@ id ='_ fZl']/span/svg/path'的元素"," request:{" headers:{" Accept:" application/json,image/png," Connection:"关闭," 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":"/element","urlParsed":{"anchor":","query":" ,文件":元素",目录":"/",路径":"/元素",相对":"/元素",端口":",主机":","password:"," user:"," userInfo:"," authority:"," protocol:"," source:"/element," queryKey:{}, "chunks":["element"]},"urlOriginal":"/session/feab13f0-720f-11e7-80b3-452aee308158/element"}}

Additional information: {"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":"/element","urlParsed":{"anchor":"","query":"","file":"element","directory":"/","path":"/element","relative":"/element","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/element","queryKey":{},"chunks":["element"]},"urlOriginal":"/session/feab13f0-720f-11e7-80b3-452aee308158/element"}}

还有其他隐藏FirefoxDriver的方法吗? 你能帮我吗?

Is there any another way for hiding FirefoxDriver? Could you help me please?

推荐答案

我解决了. 首先 我们可以通过以下代码使用PhantomJS而不显示其控制台:

I solved it. First of all We can use PhantomJS without showing its console by this code:

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

第二个是我提到的错误. Google为浏览器返回了不同的HTML页面,因此PhantomJS浏览器中的Id或Xpath与我打开Firefox时导出的ID或Xpath会有所不同. 当我使用

Second for the error that I mentioned. Google return different HTML pages for browsers so the Id or Xpath in PhantomJS browser will be different from that I export it when I was opening Firefox. When I used

string html=driver.PageSource;

要知道正确的XPath或Id,findElement函数运行良好.

to know what the correct XPath or Id, findElement functiom is working well.

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

For example: For the Google site results The first link's XPath in FirefoxDriver is

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

PhantomJSDriver中第一个链接的XPath是

The first link's XPath in PhantomJSDriver is

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

这篇关于如何在PhantomDriver(无头浏览器)中隐藏FirefoxDriver(使用Selenium)而没有findElement函数错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆