Selenium Form WebElement: In our post, we have seen how we can locate single and multiple elements with the help of Findelement and Findelements. Now in this post how we can access the various elements of a web form. As we know that for better looks of the online web pages we are using so that in a single form we can add so many various types of elements like Text boxes, Password fields, Checkboxes, Radio buttons, dropdowns, file inputs, etc.
So let’s know how we can deal with those elements with the help of Selenium WebDriver with Java. As in our previous post, we have seen WebDriver treat every element as an object of WebElement. So here we will see how to handle those elements and different available methods which helps in accessing those elements of a form.
Selenium Form WebElement
f you have not to take a look of our article regarding how you can able to locate elements on a web page with the help of Findelement and Findelements then we recommend to read the article first and after that read this article so that you can easily understand the how we have used those methods. If you have read that article then we can start with real-time examples of form elements like below:
- How to Handle Input Box
- How you Can enter Value in a Text Box
- How you can Delete the Values of a Text Box
- How to Deal With Buttons
How to Handle Input Box
We use Input Box to receiving the data from the outer worlds. There are two types of input boxes are present, that is:
- Text Fields: This type of text boxes will accept the value and also display the value to the user
- Password Text Box: This type of text boxes will accept the value but display those values in special characters to avoid displaying sensitive values.
How you can enter data into Input Box
After locating the element uniquely we can use a special method which helps us to enter the specific data with the help of sendkeys() method. Let’s see the below example how we can enter data into the text box
Deleting Values in Input Boxes
Similarly sendkeys() method there is a method present to clear the data in a text box. for that first we need to uniquely identify the elements after that we have to call the clear() method. This method does not require any parameter like sendkeys() method. you can see the below example for code snippet:
How to Deal with Buttons
For accessing the button we can take help of any of the below two methods
- Click(): You can use this method to click a button on a web application. click() method will work on any of this elements type like type=”button” or type=”submit”.
- Submit(): If you have look on the element tab of the console and if you have found the type=”submit” then, in that case, submit() method is a very good alternative of click method.
- If any form button does not have the type=”submit” then the submit() will not be work.
- If the button is not inside the <form> tag then also the submit() method will not work.
Difference between Click()and Submit()
|You can use .click() method to click on any button.There is no restriction for click buttons.||we can use .submit() method for only submit form after click on button.|
|That means element’s type = “button” or type = “submit”, .click() method will works for both.||That means element’s type = “submit” and the button should be inside <form> tag, then only submit() will work.|
|If button is inside <form> tag or button is outside <form> tag, the click() method will work.||If element’s type = “button” means submit() will not work.|
If button outside of the <form> tag means submit() will not work
|When you perform Click operation on an element. then it causes a new page to load and discard all references to this element and any further operations performed on this element will throw a StaleElementReferenceException.||If this current element is a form or an element within a form, then this will be submitted to the remote server. If this causes the current page to change, then this method will block until the new page is loaded.|