0

The website has standard image which i can use to identify the xpath. but i want to click on the third image which contains specific text only. the picture is beside the image, it that fine to have img and text in one command line?

  • Original working code:

    driver.findElement(By.xpath("(//img[@class='s-image'])[2]")).click();
    
  • Try to insert the contains command but it does not work:

    driver.findElement(By.xpath("(//img[@class='s-image'])[2] and contains(text(),'Apple MacBook Pro')")).click();
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
user2201789
  • 1,083
  • 2
  • 20
  • 45

1 Answers1

2

You have tried wrong option that is not text that is alt property to get into xpath.Try now.

driver.find_element_by_xpath("(//img[@class='s-image'][contains(@alt,'Apple MacBook Pro')])[2]").click()

OR

driver.find_element_by_xpath("(//img[@class='s-image'][starts-with(@alt,'Apple MacBook Pro')])[2]").click()
KunduK
  • 32,888
  • 5
  • 17
  • 41
  • above is the link for clear picture. how to click on the second image in the list? – user2201789 May 01 '19 at 11:37
  • @user2201789 : Edited the answer.Check now.You have to select alt property of image.there is no text attribute of image tag. – KunduK May 01 '19 at 11:59
  • Sorry provided you python code but xpath remain same in Java as well just use the xpath not the entire code. My mistake.I’ll update it later or you can edit I am away now can’t change ATM. – KunduK May 01 '19 at 12:16
  • i trying to modify to java. but able to. not sure what else missing – user2201789 May 01 '19 at 12:27