【发布时间】:2021-06-15 05:38:52
【问题描述】:
我只想访问 xpath。但是当我在检查菜单中搜索它时,它会找到 5 个结果。我想访问其中一个而不是 5。我怎样才能用索引号呢?喜欢...
xpath = "//a[@role='button']" # 5 elements available with this xpath
modified_xpath = "//a[@role='button'][2]" # I'm trying with the index number.
driver.find_element_by_xpath(modified_xpath).click() # But It's not working.
它不工作!
【问题讨论】:
-
find_elements_by_xpath返回一个列表,删除xpath中的索引查找并索引到返回的列表中?driver.find_elements_by_xpath(modified_xpath)[2] -
错误是什么?分享页面的 HTML 或 URL
-
显示这个错误
selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: Unable to locate an element with the xpath expression //a[@role='button'] because of the following error: SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//a[@role='button']' is not a valid XPath expression.