Selenium - 找到元素

时间:2018-03-20 15:12:21

标签: python python-3.x selenium selenium-webdriver webdriver

解释find_element_by_xpath(my_xpath)find_element(By.XPATH, my_xpath)之间的区别。

from selenium import webdriver
from selenium.webdriver.common.by import By

source = "https://github.com/"
my_xpath = '/html/body/div[4]/div[2]/div[2]/div/div[1]/img'
driver = webdriver.Chrome()
driver.get(source)
x = driver.find_element_by_xpath(my_xpath)
y = driver.find_element(By.XPATH, my_xpath)
driver.quit()

在调试器中,我看到类似的对象x和y:

enter image description here

1 个答案:

答案 0 :(得分:4)

没有区别。

函数find_element_by_xpath(some_xpath)只是find_element(By.XPATH, some_xpath)的便捷捷径。

如果您要阅读the source,您会发现函数find_element_by_xpath实际上会调用find_element

def find_element_by_xpath(self, xpath):
    '''docstring omitted for brevity on SO'''
    return self.find_element(by=By.XPATH, value=xpath)

直到源头,完全一样。