简体   繁体   English

如何使用 Java 在 selenium WebDriver 中按 CTRL+T 和 CTRL+TAB?

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

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.对于我的一个项目,我需要打开一个新选项卡并在选项卡之间导航,我需要知道如何使用 Java 在 Selenium Webdriver 中按CTRL + TCTRL + TAB

Please let me know how can I do the same.Thank You...!!!请让我知道我该怎么做。谢谢...!

I'm using the below:我正在使用以下内容:

Firefox Version: 48.0.2火狐版本: 48.0.2

Java Version: 1.8 Java版本: 1.8

Selenium WebDriver Version: 3.0.0 Selenium WebDriver 版本: 3.0.0

OS: Windows 10操作系统: 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());      
    }
}

You can use Actions class for Ctrl + t or CTRL + TAB .您可以将 Actions 类用于Ctrl + tCTRL + 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();

}

You can use Robot class as well simply import您也可以使用 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();
            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我尝试在 Chrome 浏览器中使用 Selenium (Java) 按 Ctrl+T - I tried to press Ctrl+T using Selenium (Java) in the Chrome browser 使用 java 在 Selenium WebDriver 中按下(Ctrl + 鼠标单击) - Key press in (Ctrl + mouse click) in Selenium WebDriver using java 如何使用Selenium WebDriver按Ctrl + 0(零) - How to press Ctrl+0 (Zero) using Selenium WebDriver 如何通过 Selenium WebDriver 使用 Java 按 Ctrl+A 到 select 页面中的所有内容 - How to press Ctrl+A to select all content in a page by Selenium WebDriver using Java 如何使用 Java 在 Selenium 中按 Ctrl + Shift - How to press Ctrl + Shift in Selenium with Java 按 Ctrl+Shift+c 使用 Selenium webdriver 不工作 - Press Ctrl+Shift+c using Selenium webdriver is not working 如何使JTabbedPane不捕获Ctrl + Tab键绑定? - How to make JTabbedPane not to catch Ctrl+Tab key binding? 在Selenium Webdriver中使用Ctrl +单击组合打开新选项卡 - Opening a new tab using Ctrl + click combination in Selenium Webdriver 如何使用Java在Selenium WebDriver中按“TAB”然后按“ENTER”键? - How to press “TAB” then “ENTER” key in Selenium WebDriver using Java? 如何在不使用剪贴板或操作 CTRL+C、CTRL+V 的情况下将字符串中的“\\t”或“tab”发送到 selenium 中的文本框 - How to send “\t” or “tab” within a string into textbox in selenium without using Clipboard or Actions CTRL+C, CTRL+V
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM