简体   繁体   English

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

[英]How to click on second element from list of web element in python selenium?

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

I have written above xpath to find the web element. 我在上面写了xpath来查找web元素。 It gives me three result. 它给了我三个结果。 I want to click on second web element. 我想点击第二个网络元素。 Could you please tell me how it can be done in python selenium? 你能告诉我如何在python selenium中完成它吗?

with an xpath returning the 2nd match from all results : 使用xpath从所有结果返回第二个匹配:

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

with an xpath returning the 2nd child from the same level : 使用xpath从同一级别返回第二个子节点:

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

or with an xpath returning multiple elements: 或者使用返回多个元素的xpath:

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

or with a css selector returning multiple elements: 或者使用css选择器返回多个元素:

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

you can try: 你可以试试:

driver.find_element_by_xpath('//yourXpath/following-sibling::node()').click()

or 要么

driver.find_element_by_xpath('//yourXpath/following-sibling::theTag').click()

Where the theTag is whatever you want: div, tr, ul, ... theTag是你想要的任何东西:div,tr,ul,......

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

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