In Automation the most common term is web driver and web element but do you what exactly the meaning of this term. So here in this tutorial, I am going to cover what is Web driver in selenium and what is web element in selenium and why we use this let’s see in detail.
What is Web driver in selenium
Web driver is an Interface which contains all the abstract methods. RemoteWebDriver class implements WebDriver interface which is extended by browser drivers.
For more details, you need to understand a scenario
These days multiple browsers available in the market like Chrome, Firefox, Opera, Safari, etc. And every browser has its own way to implements things like the lunching browser, close browser minimize browser and so on.
It’s not possible for selenium developers to develop separate frameworks for every browser. And browser team also not allow to change there source code according to selenium requirement.
So they create an agent that is called browser classes Like Chrome driver, Firefox driver. These driver classes work as an interface between selenium and browsers or you can say its communication medium between selenium and browsers.
Selenium developers do not know the logic and logic of all the browser so they created WebDriver as an interface. which contain all the abstract methods without logic and implementation and gave to browsers to handle them whenever these methods are called on browsers.
RemoteWebDriver class implements WebDriver interface which is extended by browser drivers class.
So now If I want to use perform the operation with chrome browser I need to create an object of Chrome driver.
WebDriver driver = new ChromeDriver();
Now this driver object can access all the elements of the Chrome browser.
What is Web element in selenium
WebElement: In simple words, WebElement is a representation of HTML elements. HTML elements are the tag which is starts and ends with some tag. For example heading tag: <h1>Contain….</h1> here h1 is tag or we can say HTML elements. An HTML element can contain multiple elements that are called nested elements.
<!DOCTYPE html> <html> <head> <title>Demo tittle</title> </head> <body> <h1>This is heading one</h1> <input type="text" name="username" id="name" placeholder="enter your name.."> </body> </html>
To handle this elements selenium provides WebElement Interface. There are lots of useful methods in the web element interface to interact with these HTML elements and for performing operations.
NOTE: WebElement interface is implemented by RemoteWebElement, AndroidWebElement, or HtmlUnitWebElement.
Methods of WebElement Interface.
To get elements from the HTML page WebDriver provides a method is called findelement(By arg0) and this will return a elements of WebElement type.
WebElement element = driver.findElement(By.xpath(""));
Now we have elements in this element object now we need to perform action on this element so we will use the below methods.
The Followings are some important methods which are commonly used.
This method is used for only click on the elements it will not return anything. For example, we have a button element and we just need to click on that button.
WebElement button = driver.findElement(By.xpath("//*[@id=\"btn_basic_example\"]")); button.click();
This method uses to clear any input box and it will not return any value. For example, we want to clear text from any input box.
WebElement input = driver.findElement(By.xpath("//*[@id=\"user-message\"]")); input.clear();
sendKeys(CharSequence… keysToSend ) : void
This method uses to set a text value to an input box and it takes a string a parameter. For example, If we want to send any text in an input box.
WebElement input = driver.findElement(By.xpath("//*[@id=\"user-message\"]")); input.sendKeys("Hello Folks");
This method will return a text of the element. For example: Take the text of the heading.
WebElement input = driver.findElement(By.xpath("//*[@id=\"easycont\"]/div/div/h3")); String headingText = input.getText();
This method used to verify the element is enabled or disable for example we want to check a button is enable or disable.
WebElement button = driver.findElement(By.xpath("//*[@id=\"btn_basic_example\"]")); button.isEnabled();
This method is used to verify the element is displayed or not.
WebElement button = driver.findElement(By.xpath("//*[@id=\"btn_basic_example\"]")); button.isDisplayed();
This method is used to verify an element is select or not. It is widely used in checkboxes, radio boxes, and options in select.