【问题标题】:How to hide FirefoxDriver (using Selenium) without findElement function error in PhantomDriver(headless browser)?如何在 PhantomDriver(无头浏览器)中隐藏 FirefoxDriver(使用 Selenium)而不出现 findElement 函数错误?
【发布时间】:2017-12-27 05:37:19
【问题描述】:

我尝试制作隐藏的 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);

错误信息:

WebDriver.dll 中出现“OpenQA.Selenium.NoSuchElementException”类型的未处理异常

附加信息: {"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"}}

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

【问题讨论】:

    标签: c# selenium phantomjs automated-tests selenium-firefoxdriver


    【解决方案1】:

    没有办法隐藏 FirefoxDriver 本身。您可以在虚拟机上运行它并最小化 vm 窗口,但这对大多数人来说并不实用。

    让我们来看看你真正的问题。看起来 Google 正在使用 js 分配搜索框的 id 以防止抓取,因为这违反了他们的服务条款。

    你有几个选择:

    1) 使用名称 'q' 定位元素,因为无论 phantomjs 还是 firefox 都是这样命名的。

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

    【讨论】:

      【解决方案2】:

      我解决了。 首先 我们可以在不显示控制台的情况下使用 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 函数运行良好。

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

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

      PhantomJSDriver 中第一个链接的 XPath 是

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

      【讨论】:

      【解决方案3】:

      自从 Linux 版本 55+ 和 Windows 和 OSX 版本 56+ 起,Firefox 支持-headless 命令行选项。 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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-08-02
        • 1970-01-01
        • 2016-08-16
        • 2019-03-14
        • 2021-03-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多