Shoaib Akhtar
Shoaib Akhtar

Reputation: 1403

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?

Upvotes: 3

Views: 7100

Answers (2)

Florent B.
Florent B.

Reputation: 42538

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]

Upvotes: 9

Wandrille
Wandrille

Reputation: 6821

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

Upvotes: 0

Related Questions