Our Engineers can Write as well

Identify Web Elements Using Selenium Xpath Locator

As we all know, Web elements are present diversely (Dropdown, Check Box, Text Box, Radio Button, etc.) in a Web page. And locators are our saviours, right from helping us identify a Web element uniquely within the page to enunciating its HTML properties and informing Selenium about the action to be performed.

Case in Hand: Dropdown Field without Select Class

In the scenario I’ve taken for discussion (please refer to the screenshot below), the Dropdown mandatory field “Highest Qualification” is not with Select Class in the Web page. It is difficult to find the Web element for that field.

To resolve this, I’ve used Xpath Locator and found the element for the “Highest Qualification” field. Also I tried to select the Dropdown field value “Masters” making use of the Action Class. You can find the script below:

Code:

WebElement wb1 = driver.findElement(By.xpath(“.//*[@id=’educationDetailForm’]/edu-section/section/edu-qualification/div/div[1]/div/div/div[1]/ul/li/div/label/input”));
Actions action1 = new Actions(driver);
action1.moveToElement(wb1).build().perform();

As you see, using the Action Class to find the element for the mandatory “Highest Qualification” field will not be fruitful.

web elements classification

Moreover, I’ve applied the Contains text method to find out the element for the Xpath.

web elements code

driver.findElement(By.xpath(“//*[@class=’transcluded’]//*[contains(text(),’Masters’)]”)).click();

Well, here is the crux of my article: Identifying Web elements is always a sensitive area and mandates a precise attitude. Therefore I would suggest the more effective your locator, more secure will be your test automation script. Feel free to comment below and share your feedback.

Ragul Deep - software testing engineer

Contributed by:

Ragul Deep
Software Testing Engineer

Your email address will not be published. Required fields are marked *