Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access xpath with index number in selenium python?

I just want to access a xpath. But when I'm search this in inspect menu it finding 5 result. I want to access one of them not 5. How can I do it with index number? like...

xpath = "//a[@role='button']" # 5 elements available with this xpath
modified_xpath = "//a[@role='button'][2]" # I'm trying with the index number.
driver.find_element_by_xpath(modified_xpath).click() # But It's not working.

It's not working!

like image 626
Priya Avatar asked Nov 23 '25 11:11

Priya


1 Answers

You can do it like this code--

    xpath = "//a[@role='button']" 
    xpaths = driver.find_elements_by_xpath(xpath)
    xpaths[2].click()

Hope it will work.

like image 114
Jawad Avatar answered Nov 26 '25 01:11

Jawad