-2

I need use selenium to located two elements. However, they both have same span class name. My code:

select_button = driver.find_elements_by_xpath("//span[@class='mat-button-wrapper']")

The issue is: I can this command 10 times, 6 times it locates my element, ( select button), but the other 4 times, it landed on the other button. How do I make sure it is 10 out of 10 times it will located my "select " button?

George Imerlishvili
  • 1,816
  • 2
  • 12
  • 20
PChao
  • 417
  • 2
  • 5
  • 17

1 Answers1

2

In case there are 2 elements with the same xpath as you mentioned you can use (//span[@class='mat-button-wrapper'])[1] xpath to access the first element and (//span[@class='mat-button-wrapper'])[2] for the second.
However I guess it's possible to locate the desired element with relation to some other element. I will do it if you present the web page link.

Prophet
  • 32,350
  • 22
  • 54
  • 79