尝试使用以下代码打开页面上指向新标签页的任何链接并切换到该标签页。在此处执行操作,然后返回第一个选项卡以进一步执行。
WebDriver driver = new FirefoxDriver(); driver.get("http://stackoverflow.com/"); WebElement e = driver.findElement(By.xpath(".//*[@id='nav-questions']")); Actions action = new Actions(driver); action.keyDown(Keys.CONTROL).build().perform(); //press control key e.click(); Thread.sleep(10000); // wait till your page loads in new tab action.keyUp(Keys.CONTROL).build().perform(); //release control key driver.findElement(By.cssSelector("body")).sendKeys(Keys.ConTROL + "t"); //move to new tab driver.navigate().refresh(); // refresh page driver.findElement(By.xpath(".//*[@id='hlogo']/a")).click(); //perform any action in new tab. I am just clicking logo driver.findElement(By.cssSelector("body")).sendKeys(Keys.ConTROL + "t"); //switch to first tab driver.navigate().refresh(); driver.findElement(By.xpath(".//*[@id='hlogo']/a")).click();// refresh first tab & continue with your further work.I am just clicking logo


