How to press the Instagram follow button? with python

Asked

Viewed 586 times

1

I’m trying to create a robot to follow people on Instagram, but I saved on the most important part, to click to follow the person. I’m using the python language and the Selenium, gnt I’ve put from td, by class, xpath, link text, css and ND VAI. And yes, I am putting the correct codes without missing any letter or number and a waiting time of 35s, the page presses, but the follow button is not selected, always appears the error Nosuchelementexception. HELP ME PF

I’ve tried to:

seguir = driver.find_element_by_link_text("Seguir")
        seguir.click()

I’ve tried to xpath:

seguir = driver.find_element_by_xpath(//button[@class='_5f5mN       jIbKX  _6VtSN     yZn4P   ']")
        seguir.click()

I’ve also tried by class:

seguir = driver.find_element_by_class_name('_5f5mN       jIbKX  _6VtSN     yZn4P   ')
        seguir.click()

And several other ways and none will. The code of the button I want to click is this:

<span class="vBF20 _1OSdk">
    <button class="_5f5mN       jIbKX  _6VtSN     yZn4P   ">Seguir</button></span>
 

The complete code is this:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
import random

class testebot:
    def __init__(self, username, password):
        self.username = username
        self.password = password
        self.driver = webdriver.Firefox(executable_path="D:\Desktop\driver\geckodriver.exe")
    
    def entrar(self):
        driver = self.driver
        driver.get("https://www.instagram.com/")
        time.sleep(5)
        driver.execute_script("")
        campo_usuario = driver.find_element_by_xpath("//input[@name='username']")
        campo_usuario.click()
        campo_usuario.clear()
        campo_usuario.send_keys(self.username)
        campo_senha = driver.find_element_by_xpath("//input[@name='password']")
        campo_senha.click()
        campo_senha.clear()
        campo_senha.send_keys(self.password)
        campo_senha.send_keys(Keys.RETURN)
        time.sleep(15)
        driver.execute_script("window.open('https://www.instagram.com/tiffanymikaely/', '_blank')")
        time.sleep(10)
        driver.find_element_by_css_selector('button').click()

teste = testebot ('username', 'senha').entrar()

if it helps, instagram html has a flex and Event. I’m new to programming

1 answer

1


Instead of using Xpath or any other selection method, use the following:

seguir = driver.find_element_by_css_selector('button')
seguir.click()

I used this code and managed to click the button. And actually using other methods it was not possible to locate the follow button. It may be some Instagram security measure to avoid bots, but with the above code it is possible to automate this function.

  • he’s finding the button, but n clicks ;-;

  • So try this: driver.find_element_by_css_selector('button'). click(), without saving the button in a variable, and see if it can click. Which browser you are using?

  • n was tbm, firefox

  • Could you put the full code in the post? Hides passwords and confidential information

  • Okay, I’ve posted

  • I ran the code here and managed to click the follow button. I replaced the line " driver.execute_script("window.open('instagram profile url', '_Blank')")" by driver.get("instagram profile url") and it worked. It takes a while to run, so when running the program wait a few seconds after the command to click the button run.

  • 1

    VERY OBG, VERY OBG MSM, MDS OBG JOÃO VICTOR

  • 1

    you’re an angel, obg

  • Available =)

Show 4 more comments

Browser other questions tagged

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