python - 如何单击 python selenium 中 web 元素列表中的第二个元素?

标签 python selenium

el = driver.find_elements_by_xpath("//div[contains(@class,'statsprogramsgridmodal')]//div[contains(@class,'ui-grid-icon-ok')]")

我在 xpath 上面写了找到 web 元素。它给了我三个结果。我想点击第二个网络元素。你能告诉我如何在 python selenium 中完成它吗?

最佳答案

使用 xpath 从所有结果中返回第二个匹配项:

el = driver.find_element_by_xpath(
  "(//div[contains(@class,'statsprogramsgridmodal')]//div[contains(@class,'ui-grid-icon-ok')])[2]")

使用 xpath 从同一级别返回第二个 child :

el = driver.find_element_by_xpath(
  "//div[contains(@class,'statsprogramsgridmodal')]//div[contains(@class,'ui-grid-icon-ok')][2]")

或者使用 xpath 返回多个元素:

el = driver.find_elements_by_xpath(
  "//div[contains(@class,'statsprogramsgridmodal')]//div[contains(@class,'ui-grid-icon-ok')]")[1]

或者使用返回多个元素的 css 选择器:

el = driver.find_elements_by_css_selector(
  "div[class*='statsprogramsgridmodal'] div[class*='ui-grid-icon-ok']")[1]

关于python - 如何单击 python selenium 中 web 元素列表中的第二个元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46603310/

相关文章:

java - 使用 intellij IDEA 从 Selenium + JUnit 生成 ANT 结果报告

python - 使用 Python gnupg 包,我收到错误 "Homedir ' 'C :/Users/. 。 .' needs read/write permissions"

python - 为什么我在使用 Fabric python 库时会收到低级套接字错误?

python - 遍历表以找到最佳解决方案

Selenium Webdriver - 当 baseurl 包含应用程序的 IP 地址时,脚本不运行

angular - 元素点击拦截 Selenium 中的错误

python - 为什么 "conda"和 "conda-env"包无法克隆?

python - 在 Python 中从哪里开始换行?

java - 使用 sendKeys 的消息 "org.openqa.selenium.InvalidSelectorException: invalid selector: Unable to locate an element with the XPath expression"

Python Windows 身份验证用户名和密码不起作用