What is Web Element?
Anything present on the webpage is Web Element. Such as text box, button, link etc. Before performing any action in Selenium we should identify the Web Element using its characteristics given by the application developer with the help of HTML.
What are the components of Web Element?
While developing the Web Element using HTML, developer will specify following components.
- HTML Tag: Any word which is present after the ‘<’ symbol can be a HTML tag.
Example: <html,> </html,>
- Attributes: Any word present after the HTML till the ‘>’ symbol is called as Attributes. Which contain ‘=’ symbol. Left side of the ‘=’ symbol is called as ‘Property Name’. Right side of the ‘=’ symbol is called ‘Property Value’.
Example: Type=”text”, Value=”true”, Multiple= “true”, etc
- Text: Any word which is present after the ‘>’ symbol till the end of the respective HTML tag is called as Text.
Example:
company
For the above code:
- Text of the title is “Canarys Automation Pvt Ltd”
- Text of the input is not present
- Text of the body is “company”
- Text of the html is “Canarys Automation Pvt Ltd” and “company”
What are the Locators?
Locators are the html tag, attribute, text and expressions. Which are used to identify the Web Element. Locating an Web Element is essential part in Selenium WebDriver because when you wants to take some action on element, first you need to locate that specific element to perform action. In selenium there are 8 types of Locators.
Frequently used locator to locate the UI element using a unique ID. If unique ID value is not available, they will use CSS selector locator followed by XPath locator.
How to Locate/Select single/ multiple check box, single/multiple option in dropdown list, Links, Text Box and Button using Selenium Web driver?
1. Locating the Text Box and enter search text: Inspect the text box on which you wants to perform action. After inspecting you will get the below screen.
Code to Locate the Text Box and enter text:
driver.findElement (By.id (“SearchByKey”)).sendKeys (“enter search text”); |
2. Locating Button and click on it: Inspect the button which you wants to click.
Code to Locate the Button and click on it:
driver.findElement ((By.id (“btnSearch”))).click (); |
3. Selecting single/multiple check Box: Right click on check box/s and inspect. Below screen will appear.
Code to select single checkbox:
driver.findElement(By.xpath (“//input[@value=’false’]”)).click(); |
Code to select Multiple checkbox:
String Xp=”//input[@value=’false’]”;
Listallchkbx=driver.findElements (By.xpath (Xp)); int count=allchkbx.size(); for(int i=0; i<count; i=””></count;> { WebElement checkbox=allchkbx.get(i); checkbox.click(); } |
4. Selecting single/multiple list box: In order to select required option in the list box we should use selectBy() of select class. In order to do this we should perform following steps.
- Find the Web Element
- Create the object of the selected class
- Select the required option using anyone of the following method.
- SelectByVisibleText(“String Text”)
- SelectByIndex(int)
- SelectByValue(String Value)
Code to select single list box:
String WE = “html/body/div[2]/div[1]/div/div/div/form/div[1]/div/div/button”
WebElement listBox = driver.findElement (By.xpath (WE)).click (); Select select1 = new Select (listBox); select1.selectByVisibleText (“offers”); |
Code to select multiple list box:
String WE = “html/body/div[2]/div[1]/div/div/div/form/div[1]/div/div/button”
WebElement listBox = driver.findElement (By.xpath (WE)).click (); Select select1 = new Select(listBox); select1.selectByVisibleText(“offers”); select1.selectByVisibleText (“products”); select1.selectByVisibleText (“services”); select1.selectByVisibleText (“ratings”); |
5. Counting the number of links present on web page: Code to count the number of links present on webpage.
List allLink=driver.findElements(By.xpath(“//a”));
int count= allLink.size(); System.out.print(count); |