大家好,
对于我的一个项目,我需要打开一个新选项卡并在其间导航 相同的标签我需要知道如何按 CTRL + T 和 使用Java的Selenium Webdriver中的 CTRL + TAB 。
请让我知道我该怎么做。谢谢你...... !!!
我使用以下内容:
Firefox版本: 48.0.2
Java版本: 1.8
Selenium WebDriver版本: 3.0.0
操作系统:Windows 10
我尝试了下面的代码,但它似乎没有起作用:
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Handling_Tabs {
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver","C:\\Eclipse\\Drivers\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com/");
System.out.println(driver.getTitle());
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL+"t");
driver.get("http://www.bing.com/");
System.out.println(driver.getTitle());
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL+"\t");
System.out.println(driver.getTitle());
}
}
答案 0 :(得分:0)
您可以对 Ctrl + t 或 CTRL + TAB 使用Actions类。我修改了你的例子,如下所示
@Test
public void OpeningNewTab(){
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com/");
driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
System.out.println(driver.getTitle());
Actions act = new Actions(driver);
act.keyDown(Keys.CONTROL).sendKeys("t").keyUp(Keys.CONTROL).build().perform();
driver.get("http://www.bing.com/");
System.out.println(driver.getTitle());
act.keyDown(Keys.CONTROL).sendKeys("t").keyUp(Keys.CONTROL).build().perform();
driver.get("http://www.yahoo.com/");
System.out.println(driver.getTitle());
driver.close();
driver.quit();
}
答案 1 :(得分:-1)
您也可以使用Robot类,只需导入
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
public class Keyboard {
public static void main(String[] args) {
try {
Robot robot = new Robot();
// Simulate a mouse click
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
// ctrl + T & ctrl TAB
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_T);
// CTRL+T is now pressed
robot.keyRelease(KeyEvent.VK_T);
robot.keyRelease(KeyEvent.VK_CONTROL);
} catch (AWTException e) {
e.printStackTrace();
}
}