Different ways in which you can find web element using Selenium WebDriver?

Click on any of the below link to know more.

How to find web element in selenium by ID?

On Web page right click on web element for which you want to perform any action, then select “Inspect”. In DOM element gets highlighted, check Id attribute value of the element.

Web Element:


DOM:

<a id="primary-header-home" data-key="home" target="_top" href="#" data-onclick="xp.nav.trackAnalytics(this,'a','Header.Home')" data-route="/"> Home <span class="visuallyhidden">currently selected</span> </a>

WebDriver code:

WebElement element = driver.findElement(By.id(“primary-header-home“));

Syntax:

driver.findElement(By.id(<IdValue>));

Go to top ^

How to find web element in selenium by Name?

Name is the second important locator, It is useful when web element has “name” attribute and its value is unique in DOM. i.e no other web elements attribute name has same value.

Web Element:


  Note: For <input > web element “id” attribute too have unique value, “name” is used for example purpose.

DOM:

<input class="form-control" id="loanamount" name="loanamount" value="25,00,000" type="text">" data-key="home" target="_top" href="#" data-onclick="xp.nav.trackAnalytics(this,'a','Header.Home')" data-route="/"> Home <span class="visuallyhidden">currently selected</span> </a>

WebDriver code:

WebElement element = driver.findElement(By.name(“loanamount“));

Syntax:

driver.findElement(By.name(<NameValue>));

Go to top ^

How to find web element in selenium by Class attribute value?

Class Name is the third important locator, It is useful when web element has “class” attribute and its value is unique in DOM. i.e no other web elements attribute name has same value.

Web Element:

Note: “class” attribute can have two or more class names separated by space. Go for CSS or XPath in such cases.

DOM:

<button type=”submit” data-wizard-action=”{submit:true}” class=”btn-action” id=”search-button” data-tid=”search-button”> <span class=”btn-label”>Search</span> </button>

WebDriver code:

WebElement element = driver.findElement(By.className(“btn-action“));

Syntax:

driver.findElement(By.className(<ClassAttributeValue>));

Go to top ^

Link text is one more important locator, (i.e. text present within <a href=”..”>Link text</a>). It is useful when web element has links present which can be uniquely identified on Web Page. i.e no other web elements attribute name has same value.

Web Element:

DOM:

<a href=”http://www.testingbits.com/subscribe/”>Subscribe</a>

WebDriver code:

WebElement element = driver.findElement(By.linkText(“Subscribe“));

Syntax:

driver.findElement(By.linkText(<Value enclosed within <a> tag>));

Go to top ^

Find web element in selenium by using Partial link text

Partial link text is used, when using part of the link text you can easily locate web elements.

Many times it happens that link is too long and even has spaces at the end, then we go for Partial link search.

Web Element:

DOM:

<a href=”http://www.testingbits.com/hardware-bugs-fixed-software-updates/”>Could all hardware bugs be fixed by software updates?    </a>

WebDriver code:

WebElement element = driver.findElement(By.partialLinkText(“Could all hardware bug“));

Syntax:

driver.findElement(By.PartialLinkText(<Partial text enclosed within <a> tag>));

Go to top ^