7

Suppose I get a list of elements on a webpage by class name, like so:

driver.find_elements_by_class_name("something")

How can I get the XPath of each element in that list?

Note: I am not trying to find elements using XPaths. I already have the elements from the code above. I just want the XPath of each of those elements I have already found.

Eccentric
  • 81
  • 1
  • 3
  • 1
    Why do you need an XPath for an element you've already found? You've already found it... why do you need to find it again? There's no way to do what you are asking... – JeffC Jan 12 '22 at 06:09
  • All question askers doesn't ask question and then delete the question. Some are newbies and needs help in constructing the question. – undetected Selenium Jan 12 '22 at 06:13
  • @undetectedSelenium If you think the question is unclear, you should ask clarifying questions and have OP update with new info. As it's currently written, I'm pretty sure I understand the question but I could be wrong... more clarity is never a bad thing. – JeffC Jan 12 '22 at 06:30
  • It might have been a good idea to say why you want the XPATH. Because weirdly I don't think that you can get the XPATH or an element. But if you want the XPATH because you want to get the child of that element then you can just do this: `element.find_element(by=By.XPATH, value='./whatever')` The most important thing here is that you go like `element.find_element()` instead of going like `driver.find_element()` and that you have `./` instead of '/' The `./` is pretty much the XPATH of the element – ZiyadCodes Jun 18 '22 at 16:08

3 Answers3

-3

driver.find_element_by_* and driver.find_elements_by_* are deprecated. Best way to use it is as @undetected-selenium wrote.

First import By class

from selenium.webdriver.common.by import By

You can then use it with multiple location strategies.

  • XPath
    driver.find_elements(By.XPATH, "//*[@attribute='something']")
    
  • CSS Selector
    driver.find_elements(By.CSS_SELECTOR, "[attribute~=”value”]")
    

You should use this ones from now on since it's very common to get an error with the latest webdrivers saying find_element_by_* commands are deprecated

Axel519
  • 11
  • 1
  • Why add this as an answer when it doesn't add anything different than the other answers? Also, you need to read the question more carefully. OP has stated what he's looking for and this is not it. – JeffC Jan 12 '22 at 06:08
-4

WebElement identified through classname as something can also be identified using the following Locator Strategies:

  • xpath:

    driver.find_elements(By.XPATH, "//*[@class='something']")
    
  • css-selector:

    driver.find_elements(By.CSS_SELECTOR, ".something")
    
  • Note: You have to add the following imports :

    from selenium.webdriver.common.by import By
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
-5

You can install XPath Finder extension for your browser and automatically generate the unique XPath for each element.

After that you can use the Function :

driver.find_element_by_xpath("XPath string") .
  • OP is asking for the XPaths for already found elements... a browser extension cannot do that. – JeffC Jan 12 '22 at 06:08