Reputation: 1403
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
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
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