How to hide FirefoxDriver (using Selenium) without findElement function error in PhantomDriver(headless browser)?

Viewed 5463

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

Error message:

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

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"}}

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

3 Answers

Since version 55+ for Linux and 56+ for Windows & OSX, Firefox supports the -headless command line option. It shall be used like this:

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

The corresponding code in C# would be:

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

Because the .NET wrapper doesn't support the .headless property.

Related