How to press CTRL+T and CTRL+TAB in selenium WebDriver using Java?

自作多情 提交于 2020-07-07 06:00:05

问题


Hi All,

For one of my project I need to open a new tab and navigate between the tabs for the same I need to know how can I press CTRL+T and CTRL+TAB in Selenium Webdriver using Java.

Please let me know how can I do the same.Thank You...!!!

I'm using the below:

Firefox Version: 48.0.2

Java Version: 1.8

Selenium WebDriver Version: 3.0.0

OS: Windows 10

I tried the below code but it doesn't seems to be working:

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());      
    }
}

回答1:


You can use Actions class for Ctrl+t or CTRL+TAB. I modified your example as shown below

@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();

}



回答2:


You can use Robot class as well simply import

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();
            }
        }


来源:https://stackoverflow.com/questions/39338773/how-to-press-ctrlt-and-ctrltab-in-selenium-webdriver-using-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!