Sangeetha
Sangeetha

Reputation: 31

Starting Edge Chromium in InPrivate mode / headless mode did not work for selenium

I'm trying to start edge chromium in private mode during my selenium / java test.I also want to start it in headless mode. I tried some options, but none of them are working.

As per Microsoft site, (https://learn.microsoft.com/en-us/microsoft-edge/webdriver-chromium?tabs=c-sharp) i passed UseChromium property as true. But still i am unable to get the headless and private browsing to work. Please provide your ideas.

Edge chromium version: Version 81.0.416.77 (Official build) (64-bit) msedgedriver version: 81.0.416.77 (64-bit) Selenium version: 3.141.59 (stable version)

My code:

EdgeOptions options=new EdgeOptions();
options.setCapability("UseChromium", true);
options.setCapability("InPrivate", true);
driver = new EdgeDriver(options);

Upvotes: 1

Views: 8396

Answers (1)

Zhi Lv
Zhi Lv

Reputation: 21423

We could add argument inprivate to make Edge Chromium open in Private mode using Selenium WebDriver. And, add arguments headless and disable-gpu to make Edge Chromium open in headless mode using Selenium WebDriver.

Please check the following steps:

Step 1

Download the Java/C# binding of Selenium 4.00-alpha05 from here.

Download the matching version of Microsoft Edge Driver from this page.

Step 2:

Open Edge Chromium in Private mode using Selenium WebDriver:

Java Code:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.edge.EdgeOptions;

public class Edgeauto {
    public static void main(String[] args) { 
        System.setProperty("webdriver.edge.driver", "D:\\webdriver\\msedgedriver.exe"); 
        EdgeOptions edgeOptions = new EdgeOptions();
        edgeOptions.addArguments("-inprivate");
        WebDriver driver = new EdgeDriver(edgeOptions); 
        driver.get("https://bing.com");
    }
}

C# code:

using OpenQA.Selenium.Edge;
using System.Threading;

namespace ecwebdriver
{
    public class edgewebdriver
    {
        static void Main(string[] args)
        {
            EdgeOptions edgeOptions = new EdgeOptions();
            edgeOptions.UseChromium = true;
            edgeOptions.BinaryLocation = @"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe";
            edgeOptions.AddArgument("-inprivate");
            var msedgedriverDir = @"E:\webdriver";
            var driver = new EdgeDriver(msedgedriverDir, edgeOptions);
            driver.Navigate().GoToUrl("https://bing.com");
            Thread.Sleep(3000);
            driver.Close();
        }
    }
}

Open Edge Chromium in headless mode using Selenium WebDriver

C# code:

using OpenQA.Selenium.Edge;
using System.Threading;

namespace ecwebdriver
{
    public class edgewebdriver
    {
        static void Main(string[] args)
        {
            EdgeOptions edgeOptions = new EdgeOptions();
            edgeOptions.UseChromium = true;
            edgeOptions.BinaryLocation = @"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe";
            edgeOptions.AddArgument("headless");
            edgeOptions.AddArgument("disable-gpu");

            var msedgedriverDir = @"E:\webdriver";
            var driver = new EdgeDriver(msedgedriverDir, edgeOptions);
            driver.Navigate().GoToUrl("<website url>");
            Thread.Sleep(3000);
            driver.Close();
        }
    }
}

Note: Change the webdriver paths and website url in the code to your owns.

If you want start edge chromium in private mode and headless mode. the EdgeOptions should as below:

        EdgeOptions edgeOptions = new EdgeOptions();
        edgeOptions.UseChromium = true; 
        edgeOptions.AddArgument("-inprivate");
        edgeOptions.AddArgument("headless");
        edgeOptions.AddArgument("disable-gpu")

Edit

If using Selenium 3 version, we should add the Microsoft.Edge.SeleniumTools and Selenium.WebDriver packages to our .NET project using the NuGet CLI or Visual Studio. More detail information, please check this link.

Then, add the following reference:

using Microsoft.Edge.SeleniumTools;

and refer to the following C# code (remember change the path to your owns):

        EdgeOptions edgeOptions = new EdgeOptions();
        edgeOptions.UseChromium = true;
        edgeOptions.BinaryLocation = @"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe";
        edgeOptions.AddArgument("-inprivate");
        //edgeOptions.AddArgument("headless");
        //edgeOptions.AddArgument("disable-gpu");

        var msedgedriverDir = @"E:\webdriver\edgedriver_win64_81_0_416_77\edgedriver_win64";
        var driver = new EdgeDriver(msedgedriverDir, edgeOptions);
        driver.Navigate().GoToUrl("https://www.google.com");
        Thread.Sleep(3000);
        driver.Close();

[Note] the EdgeOptions and EdgeDriver are in the Microsoft.Edge.SeleniumTools namespace, instead of the OpenQA.Selenium.Edge namespace.

Upvotes: 3

Related Questions