Collections:
Submit Web Form with WebDriver in Java
How to submit Web Page Form with Selenium WebDriver in Java?
✍: FYIcenter.com
You can follow this tutorial to enter input data and submit a Web page form with Selenium WebDriver.
Several methods from Selenium WebDriver classes will be used:
1. Enter the following program, FormSubmit.java:
// FormSubmit.java
// Copyright (c) FYIcenter.com
import org.openqa.selenium.*;
public class FormSubmit {
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"));
// get the input element of the form.
WebElement query = form.findElement(By.name("Q"));
query.sendKeys("selenium");
// submit form
form.submit();
String url = driver.getCurrentUrl();
System.out.println("Test Case - Submit a search:");
System.out.println(" Actual Value: "+url);
System.out.println(" Expected Value: ..Q=selenium...");
if (url.contains("Q=selenium")) {
System.out.println(" Test Result: Passed");
} else {
System.out.println(" Test Result: Failed");
}
// pause for 1 minute before closing
try {
Thread.sleep(60*1000);
} catch (Exception e) {
}
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 \ FormSubmit.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 \ FormSubmit Chrome ... Test Case - Submit a search: Actual Value: http://sqa.fyicenter.com/index.php?Q=selenium Expected Value: ..Q=selenium... Test Result: Passed
As you can see, we are able to enter a search criteria and submit the FORM element without any problem with Chrome WebDriver.
⇒ JavascriptExecutor to Run JavaScript in Java
⇐ Retrieve Web Form Elements with WebDriver in Java
2020-01-04, ∼2415🔥, 0💬
Popular Posts:
How to perform UUDecode (Unix-to-Unix Decode)? UUEncode is Unix-to-Unix encoding used on Unix system...
What is this Website about? Welcome to the Software Quality Assurance (QA) and Testing Information C...
How to Pass Windows Environment Variable to JMeter? I want to use Windows temporary folder environme...
How to find out my browser's identification information? To help you to see your browser identificat...
How to perform UUDecode (Unix-to-Unix Decode)? UUEncode is Unix-to-Unix encoding used on Unix system...