在同一浏览器Selenium(JAVA)中同时打开多个URL



我知道如何在不同的浏览器上同时打开多个URL(Selenium在多个浏览器上的并行测试(JAVA))。但是,我的要求是在不同的选项卡中打开多个URL,但仅使用一个浏览器。有没有办法做到这一点?
这是我尝试过的,但我无法打开新选项卡。

//mainclass
public static void main(String a[])
{
    WebDriver wd = BrowserClass.getWebDriver();
    Thread t1 = new Thread(new ParallelOpenUrl(wd));
    t1.setName("https://www.google.com");
    Thread t2 = new Thread(new ParallelOpenUrl(wd));
    t2.setName("https://www.manageengine.com");
    Thread t3 = new Thread(new ParallelOpenUrl(wd));
    t3.setName("https://www.zoho.com");
    t1.start();
    t2.start();
    t3.start();
}
public class ParallelOpenUrl implements Runnable 
{
    private WebDriver wd = null;
    public ParallelOpenUrl(WebDriver wd)
    {
        this.wd = wd;
    }
    @Override
    public void run() 
    {
        try
        {
            BrowserClass.openTab(wd);
            Thread.sleep(1000);
            wd.switchTo().window(BrowserClass.getCurrentTab());
            wd.get(Thread.currentThread().getName());
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }   
        finally
        {
            wd.close();
        }
    }
}

//BrowserClass
public static String getCurrentTab(WebDriver wd)
{   
    Set<String> allTabs = wd.getWindowHandles();
    ArrayList<String> tabs = Lists.newArrayList(allTabs);
    return tabs.get(tabs.size()-1);
}
public static WebDriver getWebDriver()
{
    private WebDriver wd = null;
    if(wd == null)
    {
        System.setProperty("webdriver.chrome.driver","G:\chromedriver.exe");
        wd = new ChromeDriver();
    }
    return wd;
}
public static void openTab() throws Exception
{
    Robot rb = new Robot();
    rb.keyPress(KeyEvent.VK_CONTROL);
    rb.keyPress(KeyEvent.VK_T);
    rb.keyRelease(KeyEvent.VK_CONTROL);
    rb.keyRelease(KeyEvent.VK_T);       
}

浏览器同时打开选项卡。但一次只有一个选项卡处于活动状态。也就是说,只有在选项卡 1 完成加载 google.com 后,选项卡 2 才会开始加载 zoho.com。

您可以尝试使用javascript在新选项卡中打开网址:

driver.executescript("window.open("your url", '_blank');")

打开新选项卡和切换到新选项卡时需要小Thread.sleep()

public void openMultipleURLs() throws InterruptedException, AWTException {
            for (String url: URLs) {
                openNewTab(driver);
                switchToNewTab();
                driver.get(url);
                // do something on each web
            }
        }
    public String[] URLs = {
            "https://www.google.com", "https://www.stackoverflow.com", "https://www.microsoft.com"
    };
    public void switchToNewTab() throws AWTException, InterruptedException {
        ArrayList<String> winHandles = new ArrayList<String> (driver.getWindowHandles());
        Thread.sleep(500);
        int newTabIndex = winHandles.size();
        driver.switchTo().window(winHandles.get(newTabIndex - 1));
    }
    public void openNewTab(WebDriver driver) throws AWTException, InterruptedException {
        Robot rob = new Robot();
        rob.keyPress(KeyEvent.VK_CONTROL);
        rob.keyPress(KeyEvent.VK_T);
        rob.keyRelease(KeyEvent.VK_CONTROL);
        rob.keyRelease(KeyEvent.VK_T);
        Thread.sleep(100);
    }

最新更新