如何使用 Selenium WebDriver 開啟一個新的標籤頁?
我們可以使用 Selenium 開啟一個新標籤頁。Keys.chord 和 **sendKeys** 方法用於此目的。可以使用 Keys.chord 方法同時傳遞多個鍵。可以將一組字串或鍵作為引數傳遞給該方法。
此處將 Keys.CONTROL 和 Keys.ENTER 作為引數傳遞給 Keys.chord 方法。這被儲存為字串值,最後作為引數傳遞給 **sendKeys 方法。
語法
String nwtb = Keys.chord(Keys.CONTROL,Keys.ENTER);
driver.findElement(By.xpath("//*[text()='Company']")).sendKeys(nwtb);示例
程式碼實現。
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;
public class OpnLinkNwTab{
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
WebDriver driver = new ChromeDriver();
// wait of 4 seconds
driver.manage().timeouts().implicitlyWait(4, TimeUnit.SECONDS);
driver.get("https://tutorialspoint.tw/about/about_careers.htm");
// Keys.Chord string
String nwtb = Keys.chord(Keys.CONTROL,Keys.ENTER);
// open the link in new tab, Keys.Chord string passed to sendKeys
driver.findElement(
By.xpath("//*[text()='Company']")).sendKeys(nwtb);
}
}輸出

廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP