How to click the Instagram FOLLOW button with Selenium using python

Asked

Viewed 28 times

0

Instagram is increasingly hindering the use of automation on your site.

This code click on the first button with the text 'Follow'

You can use this to find and click:

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    driver = webdriver.Firefox(executable_path=r"./geckodriver.exe")
    url="https://www.instagram.com/PERFIL/"
    driver.get(url)

    button_follow=driver.find_elements(By.CSS_SELECTOR, 'button')

    print(len(button_follow))#Conta quantos botões encontrou

    print(button_follow[0].text)#Imprime o nome que está no botão no índice [0] da lista

    #button_follow.click()# Caso queira clicar sem conferir o texto

    if button_follow[0].text == 'Seguir': #Se o texto for 'Seguir'
        button_follow[0].click()#Clica no botão
        print('Clicou') 

I spent five hours doing this method above. I had used all these below and could not find:

button_follow =driver.find_element_by_xpath("//*[@id='react-root']/section/main/div/header/section/div[1]/div[2]/div/div/div/span/span[1]/button").click();
button_follow = driver.find_element_by_xpath('//button[contains(text(), "Seguir")]')
button_follow = driver.find_element_by_css_selector('button')
print(button_follow.text)
if button_follow.text == 'Seguir':
    button_follow.click()
Follow_Button = driver.find_element_by_xpath("//*[text()='Seguir']")
Follow_Button.click()
seguir = driver.find_element_by_css_selector('button')
seguir.click

driver.find_element_by_xpath('source=follow').click()

followButton = driver.find_element_by_tag_name('button')
print(followButton.text)
if followButton.text != 'Following': 
        followButton.click()
        time.sleep(2)
else:
    print('Não achou')
driver.find_element_by_xpath('Seguir').click()
driver.find_element_by_xpath("//*[@aria-label='{}']".format('Seguir')).click()
#driver.find_element_by_xpath("/html/body/div[1]/section/main/div/header/section/div[1]/div/div/div/a/button").click() 
#driver.find_element_by_css_selector('button._5f5mN.jIbKX._6VtSN.yZn4P').click()
#driver.find_element_by_xpath("//button[text()='Seguir']").click()
#driver.find_element_by_xpath("//button[contains(text(), 'Seguir')]").click()
#find_element_by_xpath('//button[text()="Seguir"]').click()
#wb2 = driver.find_element(By.XPATH("/html/body/div[1]/section/main/div/header/section/div[1]/div/div/div/a/button"))
#wb2.click()
sleep(10)
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.