【发布时间】:2014-05-25 13:32:51
【问题描述】:
如何使用 Selenium WebDriver 打开新标签页?
我想在新标签页中打开多个链接。这是为了尽快完成构建验证任务。因此,在每个新选项卡中都可以打开所有与烟雾测试相关的链接,然后在每个对应于烟雾测试要求的选项卡中,我们可以进行健全性测试。
【问题讨论】:
-
不是重复的......这是关于在新标签中打开一个链接 - 另一个是关于打开一个全新的标签。
如何使用 Selenium WebDriver 打开新标签页?
我想在新标签页中打开多个链接。这是为了尽快完成构建验证任务。因此,在每个新选项卡中都可以打开所有与烟雾测试相关的链接,然后在每个对应于烟雾测试要求的选项卡中,我们可以进行健全性测试。
【问题讨论】:
我们可以使用 WebDriver 的 Actions 类。见以下代码:
WebDriver driver = new FirefoxDriver();
driver.get("<provide URL>");
WebElement link = driver.findElement(locator);
Actions builder = new Actions(driver);
Action openLinkInNewTab = builder
.moveToElement(link)
.sendKeys(link, Keys.CONTROL)
.click(link)
.keyUp(Keys.CONTROL)
.build();
openLinkInNewTab.perform();
这可以循环用于多个链接。
【讨论】:
click()它时你不需要提供元素。使用sendKeys() 不会做任何事情,因为当您运行click() 时,控制键已停止按下。然后当您调用 keyUp() 时,它会崩溃,因为 Control 键已经启动。
/* 在浏览器中打开新标签 */
public void openNewTab()
{
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"t");
ArrayList<String> tabs = new ArrayList<String> (driver.getWindowHandles());
driver.switchTo().window(tabs.get(0));
}
【讨论】:
在新标签页中打开链接的唯一方法是模拟键盘快捷键。以下适用于 FFX、Chrome 和 IE
Selenium(当前)在浏览器窗口中没有任何选项卡的概念,因此要打开选项卡然后对其进行测试,您必须使用选项 3。
以下代码将执行选项 3。然后立即关闭该新选项卡。 (在 C# 中)
new Actions(WebDriver)
.KeyDown(Keys.Control)
.KeyDown(Keys.Shift)
.Click(tab)
.KeyUp(Keys.Shift)
.KeyUp(Keys.Control)
.Perform();
new Actions(WebDriver)
.SendKeys(Keys.Control + "w")
.Perform();
你也可以使用:
.MoveToElement(tab)
.Click()
在第一个选项的中间,和
.KeyDown(Keys.Control)
.KeyDown("w")
.KeyUp("w")
.KeyUp(Keys.Control)
在第二个。
【讨论】:
代码:
WebDriver wd = new FirefoxDriver();
wd.get("http://www.gmail.com");
wd.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
wd.manage().window().maximize();
//To open a new tab
Robot r = new Robot();
r.keyPress(KeyEvent.VK_CONTROL);
r.keyPress(KeyEvent.VK_T);
r.keyRelease(KeyEvent.VK_CONTROL);
r.keyRelease(KeyEvent.VK_T);
//To switch to the new tab
ArrayList<String> tabs = new ArrayList<String>(wd.getWindowHandles());
wd.switchTo().window(tabs.get(1));
//To navigate to new link/URL in 2nd new tab
wd.get("http://facebook.com");
【讨论】:
driver. 替换为wd.。由于某种原因,编辑被拒绝。
上述解决方案对我不起作用。不知道为什么,但是驱动程序会导航到新的 url,但是一个新的标签不会打开。
我终于设法使用此代码打开了新标签:
IWebDriver driver = new ChromeDriver("path for chromedriver.exe");
IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
driver.Navigate().GoToUrl("url1");
js.ExecuteScript("window.open()");
driver.SwitchTo().Window(driver.WindowHandles[driver.WindowHandles.Count - 1]);
driver.Navigate().GoToUrl("url2");
driver.WindowHandles.Count - 1 将为您提供最后打开的选项卡,即此场景中的新选项卡。希望这可以帮助某人。
【讨论】:
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.awt.AWTException;
public class Tabs {
WebDriver driver;
Robot rb;
@BeforeTest
public void setup() throws Exception {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\Anuja.AnujaPC\\Downloads\\chromedriver_win32\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.get("http://qaautomated.com");
}
@Test
public void openTab() {
//Open tab 2 using CTRL + t keys.
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"t");
//Open URL In 2nd tab.
driver.get("http://www.qaautomated.com/p/contact.html");
//Call switchToTab() method to switch to 1st tab
switchToTab();
//perform required actions on tab 1.
driver.findElement(By.xpath("//input[@id='6']")).click();
driver.findElement(By.xpath("//input[@id='plus']"));
driver.findElement(By.xpath("//input[@id='3']"));
driver.findElement(By.xpath("//input[@id='equals']"));
//Call switchToTab() method to switch to 2nd tab.
switchToTab();
//Call switchToTab() method to switch to 1st tab
switchToTab();
}
public void switchToTab() {
//Switching between tabs using CTRL + tab keys.
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"\t");
//Switch to current selected tab's content.
driver.switchTo().defaultContent();
}
@AfterTest
public void closeTabs() throws AWTException {
//Used Robot class to perform ALT + SPACE + 'c' keypress event.
rb =new Robot();
rb.keyPress(KeyEvent.VK_ALT);
rb.keyPress(KeyEvent.VK_SPACE);
rb.keyPress(KeyEvent.VK_C);
} }
本例取自THIS BLOG POST
【讨论】:
// To open a new tab to establish second player connection
((JavascriptExecutor)driver).executeScript("window.open('about:blank', '-blank')");
// To switch to the new tab
ArrayList<String> tabs = new ArrayList<String>(driver.getWindowHandles());
driver.switchTo().window(tabs.get(1));
// To navigate to new link/URL in 2nd new tab
driver.get("http://localhost:8080");
我做到了,效果很好!
【讨论】:
在第一个解决方案中,添加一个 Thread.sleep(5000) 语句。在添加 sleep 语句之前,我多次出现错误 Index out of bounds。
WebDriver wd = new FirefoxDriver();
wd.get("http://www.gmail.com");
wd.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
wd.manage().window().maximize();
//To open a new tab
Robot r = new Robot();
r.keyPress(KeyEvent.VK_CONTROL);
r.keyPress(KeyEvent.VK_T);
r.keyRelease(KeyEvent.VK_CONTROL);
r.keyRelease(KeyEvent.VK_T);
try {Thread.sleep(5000);} catch (InterruptedException e) {}
//To switch to the new tab
ArrayList<String> tabs = new ArrayList<String>(wd.getWindowHandles());
wd.switchTo().window(tabs.get(1));
//To navigate to new link/URL in 2nd new tab
wd.get("http://facebook.com");
【讨论】: