Collections:
Retrieve Web Form Elements with WebDriver in Java
How to Retrieve Web Page Form Elements with Selenium WebDriver in Java?
✍: FYIcenter.com
If a Web page is part of a Web application, it will most likely have an HTML form
to allow users to enter and process data.
This tutorial shows you how to Retrieve a Web page form and its elements with Selenium WebDriver.
Several methods from Selenium WebDriver classes will be used:
1. Enter the following program, FormElement.java:
C:\fyicenter> type FormElement.java
// FormElement.java
// Copyright (c) FYIcenter.com
import org.openqa.selenium.*;
public class FormElement {
public static void main(String[] args) {
WebDriver driver = WebDriverLoader.load(args[0]);
driver.get("http://sqa.fyicenter.com");
// get the first "form" element in the page.
WebElement form = driver.findElement(By.tagName("form"));
String action = form.getAttribute("action");
String method = form.getAttribute("method");
System.out.println("Test Case - Review FORM elements:");
System.out.println(" <form action="+action+" method="+method+">");
// get all "input" elements in the "form"
java.util.List<WebElement> list = form.findElements(By.tagName("input"));
for (WebElement input: list) {
String type = input.getAttribute("type");
String name = input.getAttribute("name");
String value = input.getAttribute("value");
String size = input.getAttribute("size");
String src = input.getAttribute("src");
String alt = input.getAttribute("alt");
System.out.println(" <input type="+type+" name="+name+" value="+value
+" size="+size+" src="/+src+" alt="+alt+">");
}
System.out.println(" </form>");
driver.quit();
}
}
2. Compile and run it with the Selenium Client JAR file.
C:\fyicenter> javac -classpath \
.;\fyicenter\selenium\java\client-combined-3.141.59.jar \
FormElement.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 \
FormElement Chrome
Starting ChromeDriver 75.0.3770.90 (a6dcaf7e...-refs/branch-heads/3770@{#1003}) on port 30996
Only local connections are allowed.
Please protect ports used by ChromeDriver and related test frameworks to prevent access by malicious code.
2:33:13 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
Test Case - Review FORM elements:
<form action=http://sqa.fyicenter.com/index.php method=get>
<input type=text name=Q value= size=28 src= alt=>
<input type=image name= value= size=20
src=http://sqa.fyicenter.com/_search.png alt=Submit>
</form>
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 \
FormElement Edge
[13:19:24.086] - Listening on http://localhost:17592/
1:19:25 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
Test Case - Review FORM elements:
<form action=http://sqa.fyicenter.com/index.php method=get>
<input type=text name=Q value= size=28 src= alt=>
<input type=image name= value= size=20
src=http://sqa.fyicenter.com/_search.png alt=Submit>
</form>
[13:19:26.655] - Stopping server.
As you can see, we are able to retrieve the FORM element and its INPUT sub-elements without any problem.
⇒ Submit Web Form with WebDriver in Java
⇐ Retrieve Web Page Title with WebDriver in Java
2020-01-04, ∼1960🔥, 0💬
Popular Posts:
How to update hidden input value field value with WebDriver in Python? Normally, a hidden input valu...
How to perform regular expression pattern match with multiple occurrences? To perform a regular expr...
How to Pass Windows Environment Variable to JMeter? I want to use Windows temporary folder environme...
How to turn on Chrome WebDriver logging with Selenium Python API? If you want to turn on logging on ...
How to convert a date and time to a standard format? Date and time can be displayed in a number of s...