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 in Selenium by Dushyant
77 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 28 by Shreya

Related Questions In Selenium

0 votes
1 answer
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 in Selenium by Abha
• 22,740 points
57 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

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,090 points
543 views
0 votes
1 answer
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,130 points
877 views
0 votes
1 answer

How to use such xpath to find web elements

It's much simpler to use: #MyDiv123 > div.super or ...READ MORE

answered Jun 14, 2018 in Selenium by Samarpit
• 5,130 points
535 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 in Selenium by Anvi
• 12,840 points
48 views
0 votes
1 answer

How can I send some data to a Prompt Alert box using Selenium Webdriver?

Hey Abhishek, you can use sendKeys() method ...READ MORE

answered Jul 2 in Selenium by Abha
• 22,740 points
55 views