如何切换至父标签到新标签并在Chrome浏览器上打开新的网址?

时间:2018-08-28 18:57:59

标签: java selenium selenium-webdriver

如何切换到父标签到新标签并在Chrome浏览器上打开新的网址?

我想在打开新标签后访问新的URL,所以我编写了如下代码,但是我可以打开新标签,系统正在父窗口中打开第二个URL,但不在第二个标签上?

getDriver().get("http://www.google.com");
String EsatwindowHandle = getDriver().getWindowHandle();

Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_T);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_T); 

Thread.sleep(5000);
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_TAB);

getDriver().get("https://in.yahoo.com");

1 个答案:

答案 0 :(得分:0)

使用List<BigDecimal> dayWiseSales; WebDrivergetWindowHandles()方法如下:

switchTo()

要切换回上一个窗口(父选项卡),请在末尾写下以下代码

WebDriver driver = getDriver();
driver.get("http://www.google.com");
String EsatwindowHandle = driver.getWindowHandle();         //get the window handler for main window

Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_T);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_T); 

Thread.sleep(5000);
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_TAB);

//Switch to new window opened
for(String winHandle : driver.getWindowHandles()){
    driver.switchTo().window(winHandle);
}

driver.get("https://in.yahoo.com");