简体   繁体   English

使用xpath按位置查找元素

[英]Find element by position using xpath

My selenium driver finds about 13 li elements. 我的硒驱动器发现了约13 li元素。 However, the following does not work, giving element not found error: 但是,以下操作不起作用,给元素未找到错误:

driver.find_element_by_xpath('//li[0]')

But, this works correctly: 但是,这可以正常工作:

driver.find_elements_by_xpath('//li')[0]

I thought these are the same, although I would prefer the first approach to work. 我认为它们是相同的,尽管我更喜欢第一种方法。 What is going on here? 这里发生了什么?

Python is zero-indexed, but XPath is one-indexed : Python是零索引的,而XPath是一索引的

The first position is 1. 第一位是1。

As a result, the following are equivalent: 结果,以下是等效的:

driver.find_elements_by_xpath('//li')[0]
driver.find_element_by_xpath('(//li)[1]')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM