我只想访问一个 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.
它不工作!
最佳答案
你可以像这段代码那样做--
xpath = "//a[@role='button']"
xpaths = driver.find_elements_by_xpath(xpath)
xpaths[2].click()
希望它能奏效。
关于python - 如何在 selenium python 中访问带有索引号的 xpath?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67980584/