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?
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")
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") .
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.
driver.find_elements(By.XPATH, "//*[@attribute='something']")
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.