Home > Software design >  Python Selenium unable to find right button
Python Selenium unable to find right button

Time:08-24

Hey Im looking for some help regarding clicking on the like buttons on the website redbubble.com I want the script to click on all the "like buttons" but the script only clicks on the same one over and over again"like and then unlike". Do you guys have any idea what im doing wrong or why it dont just go for the next button when the buttons have same name already. Right now the script are running throw a while loop, but once the "button is gone from that picture it liked" then it cant find it on any of the other pictures. And stops. For somereson I feel like the pictures have different types, but all of them goes under the same type or what I can see atlest.

Im pretty new to selenium and not sure what im doing wrong, can any one give me an helping hand plese?

Thanks in advance!

search = driver.find_element_by_xpath("//button[@type='button']").click()

This is how the site looks like: enter image description here

CodePudding user response:

You didn't show your code.
driver.find_element_by_xpath will return only the first element matching the passed locator. This is why you are clicking on the same element.
You need to use driver.find_elements_by_xpath instead.
Also, your locator is bad.
Generally, your code could be something like this:

likes = driver.find_elements_by_xpath("//button[@data-testid='favorite-button']")
for like in likes:
    like.click()
    time.sleep(0.5)

CodePudding user response:

Hey thanks for all the help, but I solved it.

  • Related