---Advertisement---

Selenium Locator Interview Questions (Level-1)

By Manisha

Published On:

---Advertisement---

Q1. What are Locators in Selenium WebDriver? Explain different types.

Answer:

Locators in Selenium are used to identify elements on a web page. Selenium WebDriver interacts with web elements like buttons, text fields, links, checkboxes, etc., using locators. These locators help Selenium scripts perform actions such as click, enter text, select, or retrieve data from elements.

Types of Locators in Selenium WebDriver:

  1. ID – By.id(“elementId”) – Fastest & most preferred if unique.
  2. Name – By.name(“elementName”) – Common but not always unique.
  3. Class Name – By.className(“className”) – Targets elements with a specific class.
  4. Tag Name – By.tagName(“input”) – Identifies elements by HTML tag.
  5. Link Text – By.linkText(“FullLinkText”) – Identifies exact anchor link text.
  6. Partial Link Text – By.partialLinkText(“PartialText”) – Uses partial text of a link.
  7. XPath – By.xpath(“//tag[@attribute=’value’]”) – Very powerful for complex paths.
  8. CSS Selector – By.cssSelector(“cssPath”) – Fast & cross-browser friendly.

Q2. What is the difference between findElement() and findElements() in Selenium?

Answer:

FeaturefindElement()findElements()
Return TypeReturns a single WebElementReturns a list of WebElements
If Not FoundThrows NoSuchElementExceptionReturns an empty list
Use CaseWhen targeting one unique elementWhen multiple elements match the locator
Exampledriver.findElement(By.id(“username”));driver.findElements(By.tagName(“input”));

👉The Next Questions-2: SELENIUM

---Advertisement---

Leave a Comment