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, 2232🔥, 0💬
Popular Posts:
How to perform UUDecode (Unix-to-Unix Decode)? UUEncode is Unix-to-Unix encoding used on Unix system...
How to turn on Chrome WebDriver logging with Selenium Python API? If you want to turn on logging on ...
How to turn on Chrome WebDriver logging with Selenium Python API? If you want to turn on logging on ...
How to Override a JMeter Variable from Command Line? I want to run the same test plan multiple times...
How to generate ISBN numbers? To help you to obtain some ISBN numbers for testing purpose, FYIcenter...