使用 Java 的 Selenium WebDriver 获取页面标题。

seleniumautomation testingtesting tools更新于 2026/1/6 19:37:17

我们可以使用 Selenium webdriver 获取页面标题。方法 getTitle() 用于获取当前页面标题,然后在控制台中获取结果。

语法

t = driver.getTitle();

让我们查找当前页面的标题。我们将得到 About Careers at Tutorials Point – Tutorialspoint 作为输出。

示例

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");
      WebDriver driver = new ChromeDriver();
      driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
      driver.get("https://www.tutorialspoint.com/about/about_careers.htm");
      // getTitle() to obtain page title
      System.out.println("Page title is : " + driver.getTitle());
      driver.close();
   }
}

输出


相关文章