在 Selenuim 中打开新浏览器标签
答案 - 我们可以在 Selenium WebDriver 中打开一个新的浏览器标签。需要使用 Keys.chord 方法和 sendKeys 方法来实现此任务。Keys.chord 方法用于一次发送多个按键。
我们应该将 Keys.CONTROL 和 Keys.ENTER 作为参数传递给该方法。完整的字符串又作为参数传递给 sendKeys。最后,sendKeys 方法应该应用于我们希望在新标签中打开的链接
语法
String l = Keys.chord(Keys.CONTROL,Keys.ENTER);
driver.findElement(By.xpath("//*[text()='Links']")). sendKeys(l);代码实现
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.Keys;
public class OpenNewTab{
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver",
"C:\Users\ghs6kor\Desktop\Java\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
// wait of 5 seconds
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
//url launch
driver.get("https://tutorialspoint.com/about/about_careers.htm");
// Keys.Chord string
String l = Keys.chord(Keys.CONTROL,Keys.ENTER);
// open new tab
driver.findElement(By.xpath("//*[text()='Terms of Use']")).sendKeys(l);
}
}广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP