How to access the second element with the same class name in selenium

I have 2 elements on my web page with the same class name, and I'm trying to access the second element, and I cannot do this. I tried [position = 1] and put [1] at the end of my xpath expression

driver.find_element_by_xpath("//div[@class='tableType value']") 

the above returns the next 2 elements

I tried

 driver.find_element_by_xpath("//div[@class='tableType value']")[1] driver.find_element_by_xpath("//div[@class='tableType value'][position=1]") 

Can someone please help me with this?

thanks

+1
source share
2 answers

Use

 driver.find_element_by_xpath("(//div[@class='tableType value'])[2]") 

or

 driver.find_element_by_xpath("(//div[@class='tableType value'])[position()=2]") 

XPath starts at 1, so the second element is at position() 2

+2
source

Hi, please find the code below to click on the second element with the same class names. [1] means that if you want to click on the second, [2] means that you want to click on the third ....

driver.find_elements_by_class_name ('class_name') [1] .click ()

0
source

Source: https://habr.com/ru/post/969754/


All Articles