Home > Software engineering >  Can't locate / interact with button on Instargram via. selenium
Can't locate / interact with button on Instargram via. selenium

Time:12-09

I was trying to create a simple python program using selenium that needs to find and click the follow button.

This is the HTML Code:

<div  id="f2d16737c928a4">

<button  type="button">Abonnieren</button>
</div>

My python code:

example = driver.find_element_by_xpath("//button[contains(@class,'sqdOP  L3NKy   y3zKF     ')]")
example.click

Every time I try it just gives me: "selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element"

If you want to take a look at the site you need to log in and go to the suggestions page where Instagram suggests people you should follow: https://www.instagram.com/explore/people/

CodePudding user response:

The XPATH you shared seems to be a randomized string and will not be easily iterated across other follow buttons. I would recommend grabbing the "full XPATH" via the HTML page that way you can have an loop-able action. See below code for a loop of clicking the follow button:

count=1
while True:
      try:
            driver.find_element_by_xpath("/html/body/div[1]/div/div/section/main/div/div[2]/div/div/div[" str(count) ")]/div[3]/button").click()
            count=count 1
      except:
            break

CodePudding user response:

driver.find_element_by_xpath("//button[.='Abonnieren']").click()

Just look for the button with that text and click it.

  • Related