Sergiu BRC
Sergiu BRC

Reputation: 17

How can I scroll with selenium if I already follow a user?

I want to make an auttomat instagram follow script with selenium and python but I am a little stuck. I ve searched all around the internet but nothing clear for me I want to enter on a user followers and click follow on all followers. How I make the system to scroll down if I am already following the user. I tried like this but I got this error: element click intercepted

# -*- coding: utf-8 -*-
"""
Spyder Editor

This is a temporary script file.
"""

from selenium import webdriver
from time import sleep
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
import time, urllib.request
import requests
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException
driver = webdriver.Chrome(r"F:\132\instagram\chromedriver.exe")

driver.get("https://www.instagram.com/")
sleep(4)
driver.find_element_by_xpath('//button[text()="Accept All"]')\
  .click()
sleep(4)
driver.find_element_by_xpath(("//input[@name=\"username\"]"))\
    .send_keys(("#"))
driver.find_element_by_xpath(("//input[@name=\"password\"]"))\
    .send_keys(("#"))
driver.find_element_by_xpath('//button[@type="submit"]')\
    .click()
sleep(7)
driver.find_element_by_xpath('//button[text()="Not Now"]')\
  .click()
sleep(5)
driver.find_element_by_xpath('//button[text()="Not Now"]')\
  .click()
  #searchbox
time.sleep(5)
searchbox=driver.find_element_by_css_selector("input[placeholder='Search']")
searchbox.clear()
searchbox.send_keys("delianomad")
time.sleep(5)
searchbox.send_keys(Keys.ENTER)
time.sleep(5)
searchbox.send_keys(Keys.ENTER)
sleep(6)
driver.find_element_by_partial_link_text("followers").click()
buttons = driver.find_element_by_xpath("//*[text()='Follow']")    
scroll = 0
sleep(10)
fBody  = driver.find_element_by_xpath("//div[@class='isgrP']")   
for btn in buttons:
     if btn.text == 'Follow':
         driver.execute_script("arguments[0].click();", btn)
         sleep(60)
     else :
         driver.execute_script('arguments[0].scrollTop = arguments[0].scrollTop + arguments[0].offsetHeight;', fBody)
   
print("Sesiune terminata")

Upvotes: 0

Views: 114

Answers (2)

Piotr Majchrowski
Piotr Majchrowski

Reputation: 26

This part of your code does not make sense:

buttons = driver.find_element_by_xpath("//*[text()='Follow']")    
scroll = 0
sleep(10)
fBody  = driver.find_element_by_xpath("//div[@class='isgrP']")   
for btn in buttons:
     if btn.text == 'Follow':
         driver.execute_script("arguments[0].click();", btn)
         sleep(60)
     else :
         driver.execute_script('arguments[0].scrollTop = arguments[0].scrollTop + arguments[0].offsetHeight;', fBody)

Please be aware, that first you are getting all elements with text 'Follow' to buttons, then you check if text of each element is in fact equal to Follow. Try to find different locator for those buttons, e.g.

//div[@aria-label='Followers']//ul//button

with this, you will get list of buttons (with and without 'Follow' text), then you can perform your actions depending if text equals 'Follow'.

Upvotes: 1

Rutu Shah
Rutu Shah

Reputation: 11

you can scroll to the element with the following code given below.

    public static void scrollElement(WebElement element) {
    Coordinates cor = ((Locatable) element).getCoordinates();
    cor.inViewPort();
    pause(2);
}

Upvotes: 0

Related Questions