facebook webdriver | list friends | | Search

The friendFacebook function performs a series of actions to navigate to a Facebook profile page, check for the "Add Friend" button, and click it to send a friend request. The function is designed to handle user login and profile navigation using Selenium WebDriver and is exported as a module for use in other code.

Run example

npm run import -- "add friend"

add friend


const getClient = importer.import("selenium client")

async function friendFacebook(driver, profile) {
  if(!driver) {
    driver = await getClient()
    await loginFacebook(driver)
  }

  let url = await driver.getCurrentUrl()
  let loggedIn = url.indexOf(profile) > -1
  if(!loggedIn) {
    if(profile.indexOf('facebook.com') == -1) {
      profile = 'https://www.facebook.com' + profile
    }

    await driver.get(profile)
    await new Promise(resolve => setTimeout(resolve, 4000))
  }


  let connectButton
  try {
    connectButton = await driver.findElements(By.xpath('//*[@role="button" and contains(., "Add friend")]'))
  } catch (e) {}

  if(!connectButton[0] || !(await connectButton[0].isDisplayed())) {
    // todo:
  } else {
    await connectButton[0].click()

    await new Promise(resolve => setTimeout(resolve, 1000))
  }

}

module.exports = friendFacebook

What the code could have been:

const { Builder, By, until } = require('selenium-webdriver');
const { setTimeout } = require('timers');
const { promisify } = require('util');

// Import selenium client and promisify setTimeout function
const getClient = async () => {
  const seleniumClient = await import('selenium client');
  const driver = await new Builder().forBrowser('chrome').build();
  return { driver, seleniumClient };
};

// Function to login to Facebook
const loginFacebook = async (driver) => {
  // Login to Facebook logic goes here
  // Replace with your actual login logic
  await driver.get('https://www.facebook.com');
  await driver.findElement(By.name('email')).sendKeys('your_email@gmail.com');
  await driver.findElement(By.name('pass')).sendKeys('your_password', seleniumClient.Keys.RETURN);
};

// Function to add friend on Facebook
async function friendFacebook(profile) {
  // Get the selenium client
  const { driver, seleniumClient } = await getClient();

  // If driver is not provided, get one and login
  if (!driver) {
    await loginFacebook(driver);
  }

  // Get the current URL
  const url = await driver.getCurrentUrl();

  // Check if user is logged in
  const isLoggedIn = url.includes(profile);

  // If user is not logged in, navigate to profile URL
  if (!isLoggedIn) {
    // If profile URL does not start with 'https://www.facebook.com', add it
    if (!profile.startsWith('https://www.facebook.com')) {
      profile = 'https://www.facebook.com' + profile;
    }

    await driver.get(profile);
    await promisify(setTimeout)(4000);
  }

  // Try to find the connect button
  try {
    const connectButton = await driver.wait(until.elementLocated(By.xpath('//*[@role="button" and contains(., "Add friend")]')), 3000);
    await connectButton.click();
    await promisify(setTimeout)(1000);
  } catch (error) {
    // Handle the error
    console.error(error);
  }

  // Return the result
  return { result: 'Friend added successfully', profile };
}

module.exports = friendFacebook;

Code Breakdown

Importing Dependencies

The code starts by importing the selenium client using the importer.import() function and assigns it to the getClient constant.

Function: friendFacebook

The friendFacebook function takes two parameters: driver and profile.

Checking for Add Friend Button

The code attempts to find an element with the XPath //*[@role="button" and contains(., "Add friend")] on the current page, which should be the "Add Friend" button. If the button is not found or not displayed, it skips the rest of the function.

Clicking the Add Friend Button

If the button is found and displayed, it clicks the button and waits for 1000 milliseconds.

Exporting the Function

The friendFacebook function is exported as a module using module.exports = friendFacebook.