如何使用WebDriver将控制从旧选项卡转移到新选项卡?假设我在新选项卡中打开一个链接,然后想要在新打开选项卡中执行一些操作。我该怎么做呢?
当我在新选项卡中打开一个链接时,该控件仍然存在于旧选项卡中。请提供解决方案。
例如:我在gmail中打开“创建帐户”链接到新选项卡,然后我尝试在表单中填写可用的文本字段,但当我运行程序时,它总是显示找不到元素
谢谢。
发布于 2014-02-28 14:20:53
new Actions(driver).sendKeys(driver.findElement(By.tagName("html")), Keys.CONTROL).sendKeys(driver.findElement(By.tagName("html")),Keys.NUMPAD2).build().perform();将控制传递给新选项卡(此脚本假设新选项卡是第二个选项卡)。
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_W);
robot.keyRelease(KeyEvent.VK_W);
robot.keyRelease(KeyEvent.VK_CONTROL);这将关闭新的选项卡。
new Actions(driver).sendKeys(driver.findElement(By.tagName("html")), Keys.CONTROL).sendKeys(driver.findElement(By.tagName("html")),Keys.NUMPAD1).build().perform();将切换回原始选项卡。
相关问题
发布于 2016-06-17 17:11:21
//declare selectLinkOpeninNewTab above the main method
static String selectLinkOpeninNewTab = Keys.chord(Keys.CONTROL,Keys.RETURN);
// to open your seession in new tab
driver.findElement(By.id("")).sendKeys(selectLinkOpeninNewTab);
driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
// to perform actions
driver.switchTo().activeElement().sendKeys(Keys.CONTROL,Keys.NUMPAD2);
// for example thease are your actions
driver.findElement(By.id("")).click();
driver.findElement(By.id("")).clicl();
// to close new tab and back to current tab
driver.findElement(By.tagName("body")).sendKeys(Keys.CONTROL+"w");https://stackoverflow.com/questions/22087543
复制相似问题