问题
I have been using selenium for a while with these two methods interchangeably.
elem = driver.find_element_by_xpath("some_xpath")
elem = driver.find_element(By.XPATH,"some_xpath")
So far both of them work. I wanted to understand what is the difference in both of them.
https://selenium-python.readthedocs.io/locating-elements.html Documentation mentions By.XPATH as private method, but did not understand it clearly.
回答1:
find_element_by_xpath('xpath')
calls find_element(By.XPATH,'xpath')
, so actually there is no real difference.
From github
def find_element_by_xpath(self, xpath):
return self.find_element(by=By.XPATH, value=xpath)
If you look at find_element()
comment though you will see it recommends to use find_element_by_xpath
Find an element given a By strategy and locator. Prefer the find_element_by_* methods when possible.
来源:https://stackoverflow.com/questions/55467209/what-is-the-difference-in-find-element-by-xpath-and-driver-find-elementsby-x