In this post we are going to automate an Button by using click method from selenium webdriver.
Let’s use chropath to inspect submit Button and identify the WebElement locator.
As it is visible that Button doesnot have an id, hence taking the xpath of that WebElement (“//button[contains(text(),’Submit’)]”). Let’s use this ID locator to click the submit button.
Click() : Click is an predefined method from Selenium webdriver class, where we can Select the button field.
Steps to Click Button Using Selenium WebDriver with Java :
- Start writing code in an Java file.
- First let’s launch the browser (say using chrome here in the example).
- Enter the Appliction under test(AUT) url in the browser.
driver.get(“http://qacreators.com/FormToFill.html”);
- Maximize the browser window.
driver.manage().window().maximize();
- To identify the Submit button we are using By.xpath(“//button[contains(text(),’Submit’)]”) (i.e xpath locator)
Let’s discuss about this line of code :
WebElement submitButton = driver.findElement(By.xpath(“//button[contains(text(),’Submit’)]”));
–> driver : is the webdriver object.
–> findElement() : Used to identify or locate the elements to perform the operation.
–> By.xpath(“//button[contains(text(),’Submit’)]”): By.xpath helps the findElement() to find the elements with xpath locator. Here in this example we are using (xpath = “//button[contains(text(),’Submit’)]”) to identify the Submit button field.
–> click() : Webdriver command used to click Submit button based on the locator.
–> WebElement submitButton : Reference of the submitButton element is stored into submitButton variable.
- To click the SubmitButton use click method. By taking the reference of WebElement i.e submitButton (sumbmitButton.click())
submitButton.click();
- Execute the script by right click run as –> Java application.
- Once the script is executed we can observe the submit button is clicked.
- Final step is to close the browser