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, 474👍, 0💬
Popular Posts:
How to find out my browser request headers? To help you to see your browser request headers, FYIcent...
How to valid IPv6 addresses? In order to help your programming or testing tasks, FYIcenter.com has d...
How to generate date and time test values? Timestamp values are frequently needed in testing almost ...
How to Pass Windows Environment Variable to JMeter? I want to use Windows temporary folder environme...
Why I am getting gzip compressed HTTP response in SoapUI? If you run a HTTP request for some Website...