gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 09:28:13 25 4
gpt4 key购买 nike

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

我试过下面的代码,但它似乎不起作用:

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

最佳答案

您可以为 Ctrl+tCTRL+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();

}

关于java - 如何使用 Java 在 selenium WebDriver 中按 CTRL+T 和 CTRL+TAB?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39338773/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com