简体   繁体   中英

How to open Microsoft Edge (Chromium) in private mode with Selenium Python 3.x?

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.

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:

在此处输入图像描述

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边缘私有

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM