What is the difference in “find_element_by_xpath” and “driver.find_elements(By.XPATH)”

痞子三分冷 提交于 2021-01-03 06:27:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!