How can I locate web elements from a web page using Selenium Webdriver

0 votes
I want to know how web elements can be located or selected from a webpage using Selenium Webdriver?
May 28, 2019 in Selenium by Dushyant
2,613 views

1 answer to this question.

0 votes

Hey @Dushyant, Locating elements in WebDriver is done by using the method “findElement(By.locator())“. You can use following Locators to identify and locate the web elements from a web page in Selenium Webdriver:

  1. ID Locator: ID’s are unique for each element so it is common way to locate elements using ID Locator. Also ID locators are the fastest and safest locators out of all locators. For eg. findElement(By.id("IdName"))

  2. Name Locator: Name locators are also used to identify the elements on our webpage. Locating elements using Name is same as locating elements using ID locator. These are not unique on a page, so test may fail, if another element with the same Name locator is present on the web page. For eg. findElement(By.name("Name"))

  3. Class Name Locator: Class Name locator gives the element which matches the values specified in the attribute name “class”. For eg. findElement(By.className("Element Class"))

  4. Tag Name Locator: Tag Name locator is used to find the elements matching the specified Tag Name. It is very helpful when we want to extract the content within a Tag. For eg. findElement(By.tagName("HTML Tag Name"))

  5. Link Text Locator: If there are multiple elements with the same link text then the first one will be selected. This Link Text locator works only on links (hyperlinks) so it is called as Link Text locator. For eg. findElement(By.linkText("LinkText"))

  6. Partial Link Text: In some situations, we may need to find links by a portion of the text in a Link Text element. it contains. In such situations, we use Partial Link Text to locate elements. For eg. findElement(By.partialLinkText("partialLinkText"))

  7. CSS Selector Locator: CSS selectors makes the execution of script faster compared to XPath locator. This locator is always the best way to locate elements on the page. Following are the some of the mainly used formats of CSS Selectors. For eg. findElement(By.cssSelector(tag#id)), findElement(By.cssSelector(tag.class))

  8. XPath Locator: XPath is designed to allow the navigation of XML documents, with the purpose of selecting individual elements, attributes, or some other part of an XML document for specific processing. XPath produces reliable locators but in performance wise it is slower compared to CSS Selector. For eg. findElement(By.xpath("XPath"))

answered May 29, 2019 by Shreya

Related Questions In Selenium

0 votes
1 answer

How can I scroll a web page in Mozilla Firefox using Selenium?

Hi Rohan, steps to scroll a webpage in ...READ MORE

answered May 14, 2019 in Selenium by Pratibha
• 3,690 points
2,884 views
0 votes
1 answer

How can I select date from a datepicker div using Selenium Webdriver?

Hey Jeevika, you can use following automation ...READ MORE

answered Jul 15, 2019 in Selenium by Abha
• 28,140 points
11,827 views
0 votes
1 answer
0 votes
1 answer

How can I select an option from a dropdown using Ruby Selenium Webdriver?

Hey Swasti, you can try following lines ...READ MORE

answered Aug 26, 2019 in Selenium by Anvi
• 14,150 points
4,307 views
0 votes
2 answers

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,500 points
12,718 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,190 points
8,612 views
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,910 points
6,685 views
0 votes
2 answers

How to use such xpath to find web elements

xpath are two types. 1) Absolute XPath:    /html/b ...READ MORE

answered Sep 3, 2020 in Selenium by Sri
• 3,190 points
7,554 views
0 votes
1 answer

How can I locate inline elements from a web page using Selenium Webdriver?

Which specific element you are trying to ...READ MORE

answered Oct 10, 2019 in Selenium by Abha
• 28,140 points
3,739 views
0 votes
1 answer

How can I store data into a cookie using Selenium Webdriver?

Hey Abhilasha, we can load a website ...READ MORE

answered Jun 19, 2019 in Selenium by Anvi
• 14,150 points
2,754 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP