如何使用 Selenium WebDriver 在 Chrome 浏览器的新的标签页中打开一个链接?
我们可以使用 Selenium webdriver 打开 Chrome 浏览器新标签页中的链接,方法是使用 Keys.chord 方法和 sendKeys 方法。Keys.chord 方法用于将多个键同时作为参数发送。
要打开一个新标签页,Keys.CONTROL 和 Keys.ENTER 被作为参数传递给 Keys.chord。最后,Keys.chord 作为参数传递给 sendKeys。
让我们点击下图中突出显示的新标签页中的 Jobs 链接 −

语法
String l = Keys.chord(Keys.CONTROL,Keys.ENTER);
driver.findElement(By.xpath ("//a[@title='Job @ Tutorials Point']")).sendKeys(l);示例
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.Keys;
public class ElementLocator{
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver",
"C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
WebDriver driver = new ChromeDriver();
//implicit wait
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
//URL launch
driver.get("https://tutorialspoint.com/index.htm");
//Keys.chord string
String l = Keys.chord(Keys.CONTROL,Keys.ENTER);
//open in a new tab
driver.findElement(By.xpath ("//a[@title='Job @ Tutorials Point']")).sendKeys(l);
}
}输出

广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP