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.
CodePudding user response:
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") .
CodePudding user response:
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
CodePudding user response:
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
