Checkout our demo site to practice selenium https://magento.softwaretestingboard.com/

0 like 0 dislike
380 views
by The go-to Tester (324 points)
closed by
How do you identify an object using selenium?

Thank You Sir.
closed with the note: Got it, sir

1 Answer

0 like 0 dislike
by The go-to Tester (181 points)

There are 8 ways you can identify object using Selenium.

  • ID
  • CLASS
  • NAME
  • TAGNAME
  • CSS Selector
  • Link
  • Partial Link text
  • XPath

In Java,

  • ID: driver.findElement(By.id("element id"))
  • CLASS:driver.findElement(By.className("element class"))
  • NAME: driver.findElement(By.name("element name"))
  • TAGNAME: driver.findElement(By.tagName("element html tag name"))
  • CSS Selector: driver.findElement(By.cssSelector("css selector"))
  • Link: driver.findElement(By.link("link text"))
  • Partial Link text: driver.findElement(By.partialLinkText("partial link text"))
  • XPath: driver.findElement(By.xpath("xpath expression"))

​In Python,

  • ID: find_element_by_id
  • CLASS: find_element_by_class_name
  • NAME: find_element_by_name
  • TAGNAME: find_element_by_tag_name
  • CSS Selector: find_element_by_css_selector
  • Link: find_element_by_link_text
  • Partial Link text: find_element_by_partial_link_text
  • XPath: find_element_by_xpath

Xpath can be of two types, absolute and relative. You an read more about it here: What is the difference between a single and double slash used in XPath?

Also, in case of you have multiple elements present with the same locator, you can use findElements to identify all elements on the page. It will return as List of WebElements in case of Java.

Hope my answer helped! Feel free to comment for further questions.


This site is for software testing professionals, where you can ask all your questions and get answers from 1300+ masters of the profession. Click here to submit yours now!

1.4k questions

1.6k answers

866 comments

1.9k users

...