如何将控制转移到Selenium WebDriver中的新选项卡

问题描述 投票:1回答:2

如何使用WebDriver将控件从旧选项卡传输到新选项卡?假设我在新选项卡中打开一个链接,然后我想在新打开的选项卡中执行一些操作。我怎样才能做到这一点?

当我在新选项卡中打开链接时,该控件仍然存在于旧选项卡中。请提供解决方案。

例如:我在gmail中打开了“创建帐户”链接到新标签,然后我尝试填写表单中可用的文本字段,但是当我运行程序时,它总是说没有找到元素

谢谢。

selenium selenium-webdriver
2个回答
0
投票
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: Open link in new tab and close tabs switch tabs using Selenium WebDriver with Java


0
投票
//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");
© www.soinside.com 2019 - 2024. All rights reserved.