如何在 PhantomDriver(无头浏览器)中隐藏 FirefoxDriver(使用 Selenium)而不出现 findElement 函数错误?

How to hide FirefoxDriver (using Selenium) without findElement function error in PhantomDriver(headless browser)?(如何在 PhantomDriver(无头浏览器)中隐藏 FirefoxDriver(使用 Selenium)而不出现 findElement 函数错误?)
本文介绍了如何在 PhantomDriver(无头浏览器)中隐藏 FirefoxDriver(使用 Selenium)而不出现 findElement 函数错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试制作隐藏的 FirefoxDriver.根据我的研究,我必须使用 PhantomJSDriver 但是当我使用 PhantomJSDriver driver.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'的元素","re​​quest":{"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":"","文件":"元素","目录":"/","路径":"/元素","相对":"/元素","端口":"","主机":"","密码":"","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 时导出的不同.当我使用

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 函数错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)