3

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

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

我已经搜索了答案,但找不到任何答案。

我得到的最接近的是如何使用 selenium 远程 webdriver 在隐身模式下启动 Edge 浏览器?

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

4

3 回答 3

2

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

DesiredCapabilities capabilities = DesiredCapabilities.edge();
capabilities.setCapability("ms:inPrivate", true);
return new EdgeDriver(capabilities);
于 2019-05-30T05:28:05.187 回答
1

我使这些功能在 Python 中像这样工作:

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

capabilities = DesiredCapabilities.EDGE
capabilities['ms:inPrivate'] = True
driver = Edge(capabilities=capabilities) 
于 2019-11-25T13:41:33.983 回答
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
}

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

于 2017-09-06T08:02:05.310 回答