我的 selenium 驱动程序找到了大约 13 个 li 元素。但是,以下内容不起作用,出现找不到元素的错误:
driver.find_element_by_xpath('//li[0]')
但是,这可以正常工作:
driver.find_elements_by_xpath('//li')[0]
我认为这些是相同的,尽管我更喜欢第一种方法。这是怎么回事?
最佳答案
Python 是零索引的,但是 XPath is one-indexed :
The first position is 1.
因此,以下是等价的:
driver.find_elements_by_xpath('//li')[0]
driver.find_element_by_xpath('(//li)[1]')
关于python - 使用 xpath 按位置查找元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49266162/