简体   繁体   English

如何使用 Selenium Python 3.x 在私有模式下打开 Microsoft Edge (Chromium)?

[英]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.有什么方法可以使用 Selenium Python 在私有模式下打开 Microsoft Edge (Chromium)。 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更新:我也尝试“-inprivate”作为建议,但它仍然在正常 window 中打开 Edge

        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. @RichEdwards 说“-inprivate”选项适用于 C# 源代码。 So I think the issue comes from python selenium library, not msedgedriver所以我认为问题来自 python selenium 库,而不是 msedgedriver

Thanks.谢谢。

I agree with the suggestion given by the @RichEdwards我同意@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.确保您使用的是正确版本的 web 驱动程序。 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.如果您可以使用最新稳定版的 MS Edge 浏览器进行测试,那就更好了。

  2. Make sure that you had installed the MS Edge Selenium tools using command below.确保您已使用以下命令安装 MS Edge Selenium 工具。

pip install msedge-selenium-tools selenium==3.141 pip 安装 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: 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.如果您从命令行启动 edge,您可以使用msedge.exe -inprivate - 这是您需要使用选项复制的内容。

This is how i can do it in c#:这就是我可以在 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:使用 python 和你的代码,试试这个参数:

        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我在 python 中运行了这个更新的代码——包括私有标签

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)我正在使用: Version 84.0.522.52 (Official build) (64-bit) - 这是最新的,没有更新(根据更新工具)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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