使用 Selenium 在 InPrivate 模式下打开 Edge

问题描述 投票:0回答:4

我正在使用 Selenium 3.4 来使用 Microsoft WebDriver 启动 Edge,该 WebDriver 现在由 Microsoft 维护。

有什么方法可以使用 Selenium 以 InPrivate 模式启动浏览器吗?

我已经寻找答案但找不到任何答案。

我得到的最接近的是如何使用 selenium 远程网络驱动程序以隐身模式启动 Edge 浏览器?

那里提到的解决方案不起作用。它仅显示与 InPrivate 中显示的选项卡相同的选项卡,但该窗口不是私有窗口。因此,信息被存储并且会话不是私有的。

selenium webdriver microsoft-edge selenium-edgedriver microsoft-webdriver
4个回答
2
投票

添加功能... 尝试下面的代码。

DesiredCapabilities capabilities = DesiredCapabilities.edge();
capabilities.setCapability("ms:inPrivate", true);
return new EdgeDriver(capabilities);

1
投票

我让这些功能在 Python 中发挥作用,如下所示:

from selenium.webdriver import Edge
from selenium.webdriver import DesiredCapabilities

capabilities = DesiredCapabilities.EDGE
capabilities['ms:inPrivate'] = True
driver = Edge(capabilities=capabilities) 

0
投票

使用以下代码,使用 java.awt.Robot 模拟组合键

CTRL+SHIFT+P
在 InPrivate 模式下打开新的浏览器窗口:

    System.setProperty("webdriver.edge.driver","D:\\Workspace\\StackOverlow\\src\\lib\\MicrosoftWebDriver.exe"); //put actual location
    WebDriver driver = new EdgeDriver();

    driver.navigate().to("https://www.google.com");
    driver.manage().window().maximize();

     Robot robot;
    try {
        // This is the actual code that opens the InPrivate window
        robot = new Robot();
        robot.keyPress(KeyEvent.VK_CONTROL);
        robot.keyPress(KeyEvent.VK_SHIFT);
        robot.keyPress(KeyEvent.VK_P);
        robot.keyRelease(KeyEvent.VK_CONTROL);
        robot.keyRelease(KeyEvent.VK_SHIFT);
        robot.keyRelease(KeyEvent.VK_P);
        Thread.sleep(3000);
    } catch (AWTException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    String parentWindowHandler = driver.getWindowHandle(); 
    String subWindowHandler = null;

    Set<String> handles = driver.getWindowHandles();
    Iterator<String> iterator = handles.iterator();
    while (iterator.hasNext()){
        subWindowHandler = iterator.next();
        driver.switchTo().window(subWindowHandler);

        System.out.println(subWindowHandler);
    }

    driver.get("https://stackoverflow.com/");
    //driver.switchTo().window(parentWindowHandler);   // Uncomment this line if you want to use normal browser back
}

请注意,我们正在使用机器人类,因此如果系统锁定,它可能无法工作。


0
投票

在 Edge 上以私有模式运行 Selenium 测试用例。 下面的代码我在 4.XX selenium+java 上尝试过。它对我来说工作得很好。

EdgeOptions options=new EdgeOptions();
options.addArguments("--inPrivate");
WebDriver driver=new EdgeDriver(options);
© www.soinside.com 2019 - 2024. All rights reserved.