andrew
andrew

Reputation: 61

How to open new incognito window in selenium python

How I can open new incognito window? I mean when an iteration will be finished I want to open new incognito window:

class JoinPage(unittest.TestCase):

@classmethod
def setUpClass(cls):

    chrome_options = webdriver.ChromeOptions()
    chrome_options.add_argument('--incognito')
    chrome_options.add_argument('--start-maximized')
    cls.browser = webdriver.Chrome('/home/andrew/Downloads/chromedriver', chrome_options=chrome_options)

def test_01_new_account_jp(self):
    with open('/home/andrew/PycharmProjects/test/jpage/accounts.csv', 'rb', ) as csvfile:
        the_file = csv.reader(csvfile, delimiter=',')
        for row in the_file:
            for i in range(2):
                self.browser.get('http://localhost:5000')
                user = row[0]
                password = row[1]
                self.browser.xpath('html/body/div[1]/div/div/div[2]/form/div[2]/div/button').click()
                self.browser.xpath(".//*[@id='Email']").send_keys(user)
                self.browser.xpath(".//*[@id='next']").click()
                self.browser.xpath(".//*[@id='Passwd']").send_keys(password)

But when I write my code like this:

class JoinPage(unittest.TestCase):


def test_01_new_account_jp(self):
    with open('/home/andrey/PycharmProjects/test/jpage/accounts.csv', 'rb', ) as csvfile:
        the_file = csv.reader(csvfile, delimiter=',')
        for row in the_file:
            chrome_options = webdriver.ChromeOptions()  
            chrome_options.add_argument('--incognito') 
            chrome_options.add_argument('--start-maximized')  
            browser = webdriver.Chrome('/home/andrey/Downloads/chromedriver',chrome_options=chrome_options) 
            actions = ActionChains(browser)
            wait = WebDriverWait(browser, 20)
            browser.implicitly_wait(30)
            browser.get('http://localhost:5000')
            user = row[0]
            password = row[1]
            browser.xpath('html/body/div[1]/div/div/div[2]/form/div[2]/div/button').click()
            browser.xpath(".//*[@id='Email']").send_keys(user)
            browser.xpath(".//*[@id='next']").click()
            browser.xpath(".//*[@id='Passwd']").send_keys(password)

Work. I know this is not right to write like this.

Upvotes: 1

Views: 2163

Answers (1)

Apoorva Soni
Apoorva Soni

Reputation: 43

You can try this code for commonly used browsers. Hope this helps.

  • Chrome:

    DesiredCapabilities capabilities = DesiredCapabilities.chrome();
    ChromeOptions options = new ChromeOptions();
    options.addArguments("incognito");
    capabilities.setCapability(ChromeOptions.CAPABILITY, options);
    
  • FireFox:

    FirefoxProfile firefoxProfile = new FirefoxProfile();    
    firefoxProfile.setPreference("browser.privatebrowsing.autostart", true);
    
  • Internet Explorer:

    DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
    capabilities.setCapability(InternetExplorerDriver.FORCE_CREATE_PROCESS, true); 
    capabilities.setCapability(InternetExplorerDriver.IE_SWITCHES, "-private");
    
  • Opera:

    DesiredCapabilities capabilities = DesiredCapabilities.operaBlink();
    OperaOptions options = new OperaOptions();
    options.addArguments("private");
    capabilities.setCapability(OperaOptions.CAPABILITY, options);
    

Upvotes: 1

Related Questions