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

Viewed 5821
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?

2 Answers

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

Related