如何使用 Selenium webdriver 獲取頁面標題?
我們可以使用 Selenium webdriver 獲取頁面標題。方法 getTitle() 用於獲取當前頁面的標題,然後我們可以獲取控制檯中的結果。
語法
t = driver.getTitle();
讓我們找到當前頁面的標題。我們將得到關於教程點的職業生涯 – 教程點作為輸出。
示例
程式碼實現。
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 PageTitle{ public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe"); //implicit wait driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); WebDriver driver = new ChromeDriver(); //application launch driver.get("https://tutorialspoint.tw/about/about_careers.htm"); // getTitle() to obtain page title System.out.println("Page title is : " + driver.getTitle()); driver.close(); } }
輸出
廣告