How to submit a form in Selenium webdriver if submit button can't be identified?

SeleniumAutomation TestingTesting Tools

We can submit a form in Selenium webdriver even if the submit button cannot be identified. This can be achieved by locating any element within the form tag and the applying submit method on it. A form in the html code is identified by the <form> tag.

Let us investigate the html code of element with in a form tag −

In the above example, we shall try to submit the form with the help of the Email or Password field and not by clicking on the Sign in button.

Syntax

driver.findElement(By.className("input__input")).sendKeys("96968547");
driver.findElement(By.className("session_password")).sendKeys("test123");
driver.findElement(By.className("session_password")).submit();

Example

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import java.util.concurrent.TimeUnit;
public class SubmitFrm{
   public static void main(String[] args) {
      System.setProperty("webdriver.gecko.driver",
         "C:\\Users\\ghs6kor\\Desktop\\Java\\geckodriver.exe");
      WebDriver driver = new FirefoxDriver();
      //implicit wait
      driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
      //URL launch
      driver.get("https://www.linkedin.com/");
      // identify element within form
      WebElement m=driver.findElement(By.id("session_key"));
      m.sendKeys("96968547");
      WebElement n=driver.findElement(By.id("session_password"));
      n.sendKeys("test12");
      //submit form
      n.submit();
      Thread.sleep(1000);
      System.out.println("Page title: " + driver.getTitle());
      driver.close();
   }
}

Output

raja
Published on 06-Apr-2021 11:12:44
Advertisements