简体   繁体   中英

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. 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?

with an xpath returning the 2nd match from all results :

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 :

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:

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:

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, ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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