Problem in the "While" loop: the list of a page with Selenium does not scroll down

Viewed 82

A personal study purpose for my learning, I'm creating this short script which is meant to open a link to a Facebook post and click all the "Add Friend" buttons while the scroll bar scrolls through the list. To avoid spam, I added the ability to choose the maximum number of people to add and the time between requests.

enter image description here

I have the executable and complete code (published at the end of the question), but there is something wrong. How can I scroll down the list and at the same time click on the "Add friend" buttons in relation to the settings to choose the maximum number of people to add and the time between one request and another?

(SCROLL DOWN CORRECTLY) If I just write, like this, then the scrollbar goes down

i = 1

while True:
i = i + 1

row = wait.until(EC.visibility_of_element_located((By.XPATH, f"(//div[@data-visualcompletion='ignore-dynamic' and not (@role) and not (@class)])[{i}]")))

driver.execute_script("arguments[0].scrollIntoView(true);", row)

(NOT SCROLL DOWN CORRECTLY) While, for example, if I add the staves to click on all the buttons, the scroll bar no longer drops (click automatically only on the first "Add friend" button, so you don't click on the following ones, I would also like to click on the following buttons)

wait = WebDriverWait(driver, 30)
i = 1

while True:
i = i + 1

row = wait.until(EC.visibility_of_element_located((By.XPATH, f"(//div[@data-visualcompletion='ignore-dynamic' and not (@role) and not (@class)])[{i}]")))
add = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Add friend']"))).click()
close_popup = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Ok']"))).click()

driver.execute_script("arguments[0].scrollIntoView(true);", row, add, close_popup)

This is full code:

import tkinter as tk                    
from tkinter import ttk
from tkinter import *
from time import sleep
import time
import tkinter as tk
from tkinter import ttk

from selenium.webdriver import Firefox
from selenium.webdriver.firefox.service import Service
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
import os

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile

root = tk.Tk()
root.title("...")
root.geometry('530x500')
root.configure(bg='white')

topbar = tk.Frame(root, bg='#3b589e', height=42, width = 660)
topbar.place(x=1, y=1)

secondbar = tk.Frame(root, bg='#f0f2f5', height=42, width = 660)
secondbar.place(x=1, y=40)

#GUI
label_email = Label(root, text="email", bg='#3b589e', foreground="white")
label_email.place(x=2, y=10)
email = tk.Entry(root)
email.place(x=50, y = 9)

label_password = Label(root, text="password", bg='#3b589e', foreground="white")
label_password.place(x=260, y = 10)
password = tk.Entry(root)
password.place(x=335, y = 9)

link_label = Label(root, text="Post Link", bg='#f0f2f5', foreground="black")
link_label.place(x=2, y = 52)
link = tk.Entry(root, width = 50)
link.place(x=97, y = 50)

link.insert(tk.END, "https:/www.facebook.com/FranzKafkaAuthor/posts/3985338151528881") #example link

number_requests_label = Label(root, text="How many requests to send?", bg='white', foreground="black")
number_requests_label.place(x=2, y = 110)
number_requests = tk.Entry(root)
number_requests.place(x=4, y = 130)
number_requests.insert(tk.END, "10")

time_label = Label(root, text="Seconds between requests?", bg='white', foreground="black")
time_label.place(x=2, y = 180)
time_request = tk.Entry(root)
time_request.place(x=4, y = 200)


def start_with_limits(): 
    # get the values from the textboxes and convert them to integers
    delay = int(time_requests.get())
    limit = int(number_requests.get())

    #Access Facebook
    profile_path = '/usr/bin/firefox/firefox'

    options=Options()
    options.set_preference('profile', profile_path)
    options.set_preference('network.proxy.type', 4)
    options.set_preference('network.proxy.socks', '127.0.0.1')
    options.set_preference('network.proxy.socks_port', 9050)
    options.set_preference("network.proxy.socks_remote_dns", False)

    service = Service('/home/xxx/bin/geckodriver')
    driver = Firefox(service=service, options=options)
    driver.set_window_size(600, 990)
    driver.set_window_position(1, 1)
    driver.get("http://www.facebook.com")

    #Cookies before login
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'button[data-cookiebanner="accept_button"]'))).click()

    #Login
    username_box = driver.find_element(By.ID, 'email')
    username_box.send_keys(email.get())
    password_box = driver.find_element(By.ID, 'pass')
    password_box.send_keys(password.get())

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '/html/body/div[1]/div[2]/div[1]/div/div/div/div[2]/div/div[1]/form/div[2]/button'))).click()

    # --OPTIONAL
    #Cookies before login (Sometimes it is required while sometimes not
    #WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div[aria-label='Allow all cookies'] span span"))).click()


    #Open link
    driver.get(link.get())

    #Click on icon-like
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[@class='j1lvzwm4']"))).click()
    time.sleep(1)

    #Click su ALL
    WebDriverWait(driver, 2000).until(EC.element_to_be_clickable((By.XPATH, '/html/body/div[1]/div/div[1]/div/div[4]/div/div/div[1]/div/div[2]/div/div/div/div[1]/div/div/div/div/div[2]/div[2]'))).click()
    time.sleep(1)

    #Scroll down and press "Add friend" buttons
    wait = WebDriverWait(driver, 30)
    i = 1

    while i <= limit:
        i = i + 1

        row = wait.until(EC.visibility_of_element_located((By.XPATH, f"(//div[@data-visualcompletion='ignore-dynamic' and not (@role) and not (@class)])[{i}]")))
        add = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Add friend']"))).click()
        close_popup = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Ok']"))).click()
        
        driver.execute_script("arguments[0].scrollIntoView(true);", row, add, close_popup)
        time.sleep(delay)


begin = Button(root, text="Start", bg='#3b589e', foreground='white', width=7, command= start_with_limits)
begin.place(x=1, y=420)

root.mainloop()
0 Answers
Related