Keep scrolling until Python elements are discovered

My goal is to create a Facebook script that can automatically add all friends from my "Friend List" who are not already on my friend's list. However, the issue arises when there are mutual friends at the top of the list. I need the script to scroll down until it reaches the "Add Friend" button for new connections, and then proceed to click on it.

Since I am new to programming, I would appreciate detailed instructions on how to achieve this task. I understand that Java is typically used for this purpose, but could someone please explain how to implement this in Python?

Below is the initial code snippet:

root.get(targets_url)        #targets_url refers to the link of the target friend list
time.sleep(10)

while True:
    element = root.find_element_by_class_name('FriendRequestAdd').click()

Answer №1

You don't have to use Java for this task as Python will do just fine. If you want to locate the add friends element, you can simply use the find_element_by_id method or any other method available. However, if you are determined to find and scroll to the element.

from selenium.webdriver.ui.support import expected_conditions as ec
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By

wait = WebDriverWait(root)     

root.wait(3) #wait for element to be visible. implicit wait.

#you can change XPATH to CSS_SELECTOR or any other suitable locator
friendlist = wait.until(ec.element_to_be_clickable((By.XPATH,'xpath value'))  #explicitly wait for element to appear on the page
root.execute_script("arguments[0].ScrollIntoView;", friendlist)
friendlist.click()

The purpose of the wait is to ensure that the add friend id loads before Selenium starts looking for it, especially if it doesn't load immediately with the page. If Selenium still cannot find add friend, I would suggest using expected_condition and WebDriverWait, although it's unlikely to come to that

If an explicit wait doesn't work, consider checking if that section of the page is within an iframe or a different frame

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Sorting options tailored for arrays within arrays

Here is an array with nested arrays: var array = [ ['201', 'Tom', 'EES', 'California'], ['189', 'Charlie', 'EE', 'New Jersey'], ['245', 'Lisa', ' ...

Is there a browser-friendly alternative to `fs.readFileSync` function?

When I use fs.readFileSync to read a wasm file in Node, I am able to get the file in the desired format. However, when I attempt the same process in the browser using the FileReader, the format seems to be incorrect. Why does this happen? Is there an equi ...

What is the best way to extract a single-word object from an array without any special characters?

Is there a way to extract a list of single words from wordnet without any special characters included? I'm attempting to achieve something like this: const wordnet = require('wordnet') await wordnet.init(); let results = await wordnet.lis ...

Struggling to get a basic HTML form to function with JavaScript commands

In my form, there are two input fields and a button. Upon clicking the button, a JavaScript function is triggered which multiplies the values entered in the inputs. The result is then displayed in a <p> element and evaluated through an if else statem ...

Sending a JavaScript variable to PHP and retrieving the PHP result back into a JavaScript variable

I am looking to transfer a JavaScript variable to a PHP file using AJAX, and then retrieve the PHP file's output back into a JavaScript variable. For sending the JavaScript variable to the PHP file, I found a method that suggests using ajax to assign ...

When provided with no input, the function will output true

I'm having trouble understanding this problem! I've implemented a basic jQuery validation that checks if an input field is empty when a button is clicked. http://fiddle.jshell.net/fyxP8/3/ I'm confused as to why the input field still retu ...

Swapping out data points using JQuery

What could be causing line 10 to return null? Click here for the code file The code seems to function properly with line 40, but not with line 10. ...

Encountering an Enumeration Exception when trying to delete an element from a collection list using a foreach loop

I encountered an issue when attempting to remove an element from a list collection using foreach. I have searched online but have not found a clear explanation for the problem. If anyone has knowledge about this, please share the information. ...

Puppeteer is not compatible with VPS hosting on DigitalOcean

I'm currently using a droplet on DigitalOcean and encountering the following error: (node:5549) UnhandledPromiseRejectionWarning: TimeoutError: Navigation Timeout Exceeded: 300000ms exceeded at Promise.then (/var/www/screenshot/node_modules/puppe ...

Error occurs when trying to map an array within an asynchronous function

Hey there, I have an array of objects with validation inside my async function (router.post()) and I need to map it before validating. Here is the approach I am taking: ingredients.map(({ingredient, quantity})=>{ if(ingredient.trim().length < 1 | ...

I'm encountering an issue with VUEJS components including my show route in their get call. How can I make my journals/:id pages function properly without encountering a Mime

I encountered a MIME type error stating: Refused to apply style from 'http://localhost:8080/journals/assets/css/main.css' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled. ...

Unable to retrieve the textContent of an HTML element, however, it is possible to log it

Currently, I'm attempting to make changes to a table on the following website. I've inserted the script below into the console of both Chrome and Firefox: let skins = [ "button" //simplified ]; skins.forEach((skin)=>{ document.que ...

Issue encountered while attempting to save hook arrays: Uncaught TypeError - 'choices' is not able to be

I'm in the process of creating a straightforward multiple-choice exam form that includes choices and answers. Whenever a user selects an option, it should be added to the array of choices. At the start, I have an array called exercises, which consist ...

Instructions on sending search fields by pressing the enter key

Developing my application in React.tsx, I have a menu window that consists of numerous div elements with input fields, checkboxes, and select elements. Upon clicking the submit button, a list of results with user-selected filters appears. As an additional ...

The multiprocessing pool assigns each worker to run code outside the __main__ block

import multiprocessing import threading counter = 1 print("Code outside __main__",counter) lock = threading.Lock() counter += 1 def foo(i): #print("Inside foo ",i) pass if __name__ == '__main__': pool = multiprocessin ...

Embed the picture into the wall

I recently constructed a wall using three.js and would like to add an image similar to the one shown in the example below. Since I am still new to three.js, I am seeking assistance with placing the image. Can anyone provide guidance on how to do this? ...

The height of the image will be fetched only once from a large collection of images

As mentioned in the title, I have multiple elements with the same class and I am trying to fetch that class to check for the width/height/src of the child image. I am only able to retrieve the height and width of the first image, but I can get the src of ...

Determine the number of elements located inside a designated slot

Take a look at this Vue component code: <template> <!-- Carousel --> <div class="carousel-container"> <div ref="carousel" class="carousel> <slot></slot> </div> </div&g ...

Can you explain the meaning of arguments[0] and arguments[1] in relation to the executeScript method within the JavascriptExecutor interface in Selenium WebDriver?

When utilizing the executeScript() method from the JavascriptExecutor interface in Selenium WebDriver, what do arguments[0] and arguments[1] signify? Additionally, what is the function of arguments[0] in the following code snippet. javaScriptExecutor.ex ...

angularjs .reject not executing correctly within the then statement

I'm having trouble identifying the bug in my code. For some reason, $q.defer().reject() isn't functioning correctly. defer.resolve works as expected and even reaches the finally segment, but defer.reject (although it doesn't throw an error) ...