简体   繁体   中英

How to process the same step in Selenium Java using for loop for Chrome and Chrome Incognito browser?

I already have a code for this but what happens is that it opens the two browsers at the same time, failing my code to do what is intended. What I'm trying to do is the Chrome browser will execute first the process then after the first browser, the Chrome Incognito will launch and now do the same process.

I currently have this code:

chromeOptions.addArguments("--incognito");
ChromeDriver chromeIncognitoDriver = new ChromeDriver(chromeOptions);

for (ChromeDriver drivers : new ChromeDriver[] {(ChromeDriver) driver, chromeIncognitoDriver}) {

            try {
         
                   chromeIncognitoDriver.manage().window().setSize(new Dimension(412, 915));
                   drivers.get("www.google.com");
                   //do process 1st for driver then after it, chromeIncognito driver will do the same process

                 
            } catch (Exception e) {

                throw new IllegalStateException("Execution encountered an Error: " + e.getMessage());

            } finally {

                driver.close();
            }

        }
    }

}

Your code seems to be throwing illegal argument exception at below code

drivers.get("www.google.com");

This is because driver.get() method accepts only fully qualified URL(See the attached image for reference)

Change the code to:

drivers.get("https://www.google.com");

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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