0

Is there any way to open Microsoft Edge (Chromium) in private mode with Selenium Python. I tried the below code, but it does not work.

        options = webdriver.EdgeOptions()

        # try set --incognito option, but it does not work
        options.add_argument("--incognito")

        # try inprivate mode try set w3c option, but it does not work
        capabilities = DesiredCapabilities.EDGE
        capabilities['ms:inPrivate'] = True            

        self.mWebDriver = webdriver.Edge(executable_path=PATH_EDGE_WEBDRIVER, 
                                        options=options, capabilities=capabilities)

Updated: I also try "-inprivate" as suggestions, but it still open Edge in normal window

        options = webdriver.EdgeOptions()
        options.add_argument("-inprivate")

        self.mWebDriver = webdriver.Edge(executable_path=PATH_EDGE_WEBDRIVER, 
                                        options=options)

@RichEdwards said that "-inprivate" option works with C# source code. So I think the issue comes from python selenium library, not msedgedriver

Thanks.

Shymaxtic
  • 3
  • 2
  • 3
  • Does this answer your question? https://stackoverflow.com/questions/36547933/how-to-start-edge-browser-in-incognito-mode-using-selenium-remote-webdriver – Kevin Hernandez Aug 05 '20 at 13:48
  • Hi @KevinHernandez, no, it doesn't. I try to set w3c option inPrivate to True, but It seems like my Edge driver (chromium) does not support this option. – Shymaxtic Aug 05 '20 at 13:57
  • try adding the argument `-inprivate` - single dash. this is how you do it from the command line. `msedge.exe -inprivate` is what you need to replicate – RichEdwards Aug 05 '20 at 14:41
  • Does this answer your question? [Run chrome browser in inconginto Mode in Selenium](https://stackoverflow.com/questions/19026295/run-chrome-browser-in-inconginto-mode-in-selenium) – Zephyr Aug 05 '20 at 15:20

3 Answers3

1

If you launch edge from the command line you can kick off inprivate with msedge.exe -inprivate - this is what you need to replicate with the options.

This is how i can do it in c#:

case "edgechromium":
    new DriverManager().SetUpDriver(new EdgeConfig(), "83.0.478.56");
    var options = new EdgeOptions();
    options.UseChromium = true;
    options.AddArgument("-inprivate");
    b = new EdgeDriver(options);
    break;

With python and your code, try just this argument:

        options.add_argument("-inprivate") 

[update] i had a look here - there are edge tools to help here.

As per the instructions, i installed the tools:

pip install msedge-selenium-tools selenium==3.141

I ran this updated code in python - including the inprivate tag

from msedge.selenium_tools import Edge, EdgeOptions

# Launch Microsoft Edge (Chromium)
options = EdgeOptions()
options.use_chromium = True
options.add_argument("-inprivate")
driver = Edge(options = options)

driver.get ("https://www.google.com")

and i get -inprivate edge private

I'm using: Version 84.0.522.52 (Official build) (64-bit) - which is latest and no updates (according to the update tool)

RichEdwards
  • 3,423
  • 2
  • 6
  • 22
  • Hi @RichEdwards: Thanks, I tried to add the option "-inprivate" but it still opens Edge in normal mode. This means this issue comes from python selenium lib, not from the webdriver. – Shymaxtic Aug 05 '20 at 15:10
  • @Shymaxtic - have you followed these steps - installed the edge tools and such? .... https://pypi.org/project/msedge-selenium-tools/ – RichEdwards Aug 05 '20 at 15:24
  • Thank @RichEdwards, I have no idea about this library. I have tried it and thanks, it works successfully. Btw, I don't know why I cannot click to vote your answer. – Shymaxtic Aug 07 '20 at 14:24
  • No worries @Shymaxtic Happy to help :-) – RichEdwards Aug 09 '20 at 16:07
1

I agree with the suggestion given by the @RichEdwards

I suggest try to check the points below may help you to narrow down and fix the issue.

  1. Make sure you are using the correct version of the web driver. check your browser version and download the appropriate driver from here. It can be better if you can make a test with the latest stable version of the MS Edge browser.

  2. Make sure that you had installed the MS Edge Selenium tools using command below.

pip install msedge-selenium-tools selenium==3.141

Sample code:

from msedge.selenium_tools import Edge, EdgeOptions

options = EdgeOptions()
options.use_chromium = True
options.add_argument("-inprivate")
options.binary_location = r"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe"
driver = Edge(executable_path = r"D:\<driver path>\msedgedriver.exe", options = options) # Modify the path here...

# Navigate to URL
driver.get("https://example.com")

# Access web elements

driver.find_element_by_id('fname').send_keys("ABC")   

driver.find_element_by_id('lname').send_keys("XYZ")

driver.quit

Output:

enter image description here

Deepak-MSFT
  • 10,379
  • 1
  • 12
  • 19
0

You should have just switched -incognito argument to inprivate, instead of trying to do it with capabilities:

options = webdriver.EdgeOptions()
options.add_argument("-inprivate")

driver = webdriver.Edge(executable_path=PATH, options=options)

Hope this helps!