Collections:
Retrieve Web Page Title with WebDriver in Java
How to Retrieve Web Page Title with Selenium WebDriver in Java?
✍: FYIcenter.com
Once you have connected to the Web browser using the Selenium WebDriver,
you can open any remote Web page and retrieve its page title as shown in this tutorial.
3 main methods from the Selenium WebDriver class will be used:
1. Make sure you have WebDriver loading program, WebDriverLoader.java, compiled in the current directory:
C:\fyicenter> dir WebDriverLoader.*
1,693 WebDriverLoader.class
882 WebDriverLoader.java
2. Enter the following program, GetPageTitle.java, that retrieve Web page title:
C:\fyicenter> type GetPageTitle.java
// GetPageTitle.java
// Copyright (c) FYIcenter.com
import org.openqa.selenium.*;
public class GetPageTitle {
public static void main(String[] args) {
WebDriver driver = WebDriverLoader.load(args[0]);
driver.get("http://sqa.fyicenter.com");
String title = driver.getTitle();
driver.quit();
System.out.println("Test Case - Verify Page Title:");
System.out.println(" Actual Value: "+title);
System.out.println(" Expected Value: FYI Center...");
if (title.startsWith("FYI Center")) {
System.out.println(" Test Result: Passed");
} else {
System.out.println(" Test Result: Failed");
}
}
}
3. Compile and run it with the Selenium Client JAR file. Make sure the current directory is included in the "-classpath".
C:\fyicenter> javac -classpath \
.;\fyicenter\selenium\java\client-combined-3.141.59.jar \
GetPageTitle.java
C:\fyicenter> java -classpath \
.;\fyicenter\selenium\java\client-combined-3.141.59.jar;\
\fyicenter\selenium\java\libs\guava-25.0-jre.jar;\
\fyicenter\selenium\java\libs\okhttp-3.11.0.jar;\
\fyicenter\selenium\java\libs\okio-1.14.0.jar;\
\fyicenter\selenium\java\libs\commons-exec-1.3.jar \
GetPageTitle Chrome
Starting ChromeDriver 75.0.3770.8 (...-refs/branch-heads/3770@{#40}) on port 47740
Only local connections are allowed.
Please protect ports used by ChromeDriver and related test frameworks to prevent access by malicious code.
8:02:26 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
Test Case - Verify Page Title:
Actual Value: FYI Center for Software QA Testing
Expected Value: FYI Center...
Test Result: Passed
C:\fyicenter> java -classpath \
.;\fyicenter\selenium\java\client-combined-3.141.59.jar;\
\fyicenter\selenium\java\libs\guava-25.0-jre.jar;\
\fyicenter\selenium\java\libs\okhttp-3.11.0.jar;\
\fyicenter\selenium\java\libs\okio-1.14.0.jar;\
\fyicenter\selenium\java\libs\commons-exec-1.3.jar \
WebDriverLoader Edge
[20:02:53.307] - Listening on http://localhost:48540/
8:02:56 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
[20:02:58.828] - Stopping server.
Test Case - Verify Page Title:
Actual Value: FYI Center for Software QA Testing
Expected Value: FYI Center...
Test Result: Passed
As you can see, the getTitle() method works nicely.
⇒ Retrieve Web Form Elements with WebDriver in Java
⇐ Load WebDriver for Different Browsers in Java
2020-01-04, 2262🔥, 0💬
Popular Posts:
In what order thread groups are executed in JMeter? Thread groups, including regular "Thread Groups"...
How to Pass Windows Environment Variable to JMeter? I want to use Windows temporary folder environme...
Where to find online test tools? FYIcenter.com has prepared a number of online test tools: Data Look...
Where to find tutorials on Selenium test tools? I want to know how to use Selenium. Here is a large ...
How to see my IP address Host Name? To help you to see your IP Address Host Name, FYIcenter.com has ...