So, to test the form we require two main steps:
- Finding the element in the HTML code: We can use ID, class, XPath, and many more.
- Apply the operation on the field: We can write, click, and read the field or the element.
So, We will first find all the fields in the forms using their id or class whatever is available to us. Then for fields, we will send the values and for buttons, we will execute the click function in the code.
Functions for Finding the Element
As discussed we can find the element by many options so let’s have a look at some functions :
1. By Id
Java
WebElement elementById = driver.findElement(By.id( "element_id" ));
|
Python
element = driver.find_element(By. ID , "element_id" )
|
C#
IWebElement elementById = driver.FindElement(By.Id( "element_id" ));
|
2. By Class
Java
WebElement elementByClassName = driver.findElement(By.className( "element_class" ));
|
Python
element = driver.find_element(By.CLASS_NAME, "element_class" )
|
C#
IWebElement elementByClassName = driver.FindElement(By.ClassName( "element_class" ));
|
3. By Name
Java
WebElement elementByName = driver.findElement(By.name( "element_name" ));
|
Python
element = driver.find_element(By.NAME, "element_name" )
|
C#
IWebElement elementByName = driver.FindElement(By.Name( "element_name" ));
|
4. By CSS Selector
Java
WebElement elementByCssSelector = driver.findElement(By.cssSelector( "selector" ));
|
Python
element = driver.find_element(By.CSS_SELECTOR, "selector" )
|
C#
IWebElement elementByCssSelector = driver.FindElement(By.CssSelector( "selector" ));
|
5. By XPath
Java
WebElement elementByXPath = driver.findElement(By.xpath( "//input[@name='example']" ));
|
Python
element = driver.find_element(By.XPATH, "//input[@name='example']" )
|
C#
IWebElement elementByXPath = driver.FindElement(By.XPath( "//input[@name='example']" ));
|
These are some of the functions that we can use for finding the elements from the HTML. We can use different functions in different scenarios.
How to Submit a Form using Selenium?
Selenium is a great tool when it comes to testing the User interface of websites. Because it has so many features like web driver it allows us to write the scripts in different programming languages like Java, Python, C#, and Ruby. We can write scripts concerning different browsers including the major browsers like Chrome and Firefox. Submitting the form is the major task we want to do because to test different scenarios of user’s input and look at the result is very important. In HTML, there is a set of input types, and testing major of them is a difficult task but we can do this by writing a simple script that will load the site and do the major tasks.
Contact Us