[英]How to transfer control to new tab in Selenium WebDriver
如何使用WebDriver将控件从旧选项卡转移到新选项卡? 假设我在新标签页中打开了一个链接,然后在新打开的标签页中执行一些操作。 我怎样才能做到这一点?
当我在新标签页中打开链接时,该控件仍然存在于旧标签页中。 请提供解决方案。
例如:我在gmail中打开了指向“新标签”的“创建帐户”链接,然后尝试填充表单中可用的文本字段,但是当我运行该程序时,它总是说找不到元素
谢谢。
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();
将切换回原始标签。
相关问题
Selenium 2:在新标签页中打开链接并关闭标签页
使用带有Java的Selenium WebDriver切换选项卡
//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");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.