Home > other >  Open Incognito mode in selenium 4 in java
Open Incognito mode in selenium 4 in java

Time:10-01

I wanted to open a normal and incognito mode together in selenium. I could open two browsers in normal mode but I am not sure how to open the another open in incognito mode in selenium 4.

The below open the 2nd browser window in normal mode where I want this to be opened in incognito mode.

 driver.switchTo().newWindow(WindowType.WINDOW).get("URI");

Expected: 1st browser window in normal mode. 2nd browser window in incognito mode.

Actual: 1 browser opened in normal mode. 2nd browser opened in normal mode.

CodePudding user response:

That's one of the special abilities of https://github.com/seleniumbase/SeleniumBase

Here's a test you can run with pytest after doing pip install seleniumbase:

from seleniumbase import BaseCase

class MultipleDriversTest(BaseCase):
    def test_multiple_drivers(self):
        self.open("data:text/html,<h1>Driver 1</h1>")
        driver2 = self.get_new_driver(incognito=True)
        self.open("data:text/html,<h1>Driver 2</h1>")
        self.switch_to_default_driver()  # Driver 1
        self.highlight("h1")
        self.assert_text("Driver 1", "h1")
        self.switch_to_driver(driver2)  # Driver 2
        self.highlight("h1")
        self.assert_text("Driver 2", "h1")

Driver 1 will be regular Chrome. Driver 2 will be incognito Chrome. It easily switches between the two.

CodePudding user response:

WebDriverManager.chromedriver().setup();

ChromeOptions options = new ChromeOptions();
options.addArguments("--incognito");

WebDriver driver_1 = new ChromeDriver();
driver_1.manage().window().maximize();
driver_1.get("url");

WebDriver driver_2 = new ChromeDriver(options);
driver_2.manage().window().maximize();
driver_2.get("url");
  • Related