Course Name Start Date Time Duration Registration Link
No Training Programs Scheduled ClickHere to Contact
Please mail To sudhakar@qtpsudhakar.com to Register for any training

Friday, September 15, 2017

Get FaceBook Friends List Using JavaScriptExecutor

package com.wd.basics;

import java.util.List;
import java.util.concurrent.TimeUnit;
import javax.swing.JOptionPane;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Wait;
import org.openqa.selenium.support.ui.WebDriverWait;

public class FBAuto_GetFriends_Js {

 public static void main(String[] args) throws InterruptedException {
  // specify browser driver
  System.setProperty("webdriver.chrome.driver", "F:\\SeleniumSoftware\\BrowserDrivers\\chromedriver.exe");
  // open new chrome window
  WebDriver driver = new ChromeDriver();
  // maximize the browser
  // specify page load timeout
  driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
  // specify implicit wait
  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
  // navigate to facebook

  // after page loading login to facebook manually to hide the password
  JOptionPane.showInputDialog("Click on OK to Continue");

   * String uName = JOptionPane.showInputDialog("Enter UserName"); String pwd =
   * JOptionPane.showInputDialog("Enter UserName");
   * driver.findElement(By.id("email")).sendKeys(uName);
   * driver.findElement(By.id("pass")).sendKeys(pwd);
   * driver.findElement(By.xpath("//input[@value='Log In']")).click();
  // wait for some unique element in fb home window
  Wait w = new WebDriverWait(driver, 20);

  // click on user id
  // click on friends tab

  // get parent element of friends table
  WebElement elmFriendsList = driver

  // get list friends from friends table
  List elmLst = elmFriendsList.findElements(By.xpath("//div[@class='fsl fwb fcb']"));

  // initialize javascript executor
  JavascriptExecutor jsDriver = (JavascriptExecutor) driver;

  // start a flag variable to find end of friends list
  boolean itsNotEnd = true;

  // use while loop to load all friends
  while (itsNotEnd) {

   // get the last friend element in loaded friends list
   WebElement lastElem = elmLst.get(elmLst.size() - 1);

   // Move to last friend element so that it will load some friends
   jsDriver.executeScript("arguments[0].scrollIntoView();", lastElem);

   // wait for 2 seconds to load friends

   // get the updated friends list in same variable
   elmLst = elmFriendsList.findElements(By.xpath("//div[@class='fsl fwb fcb']"));

   // get the new last friend element
   WebElement nLastElem = elmLst.get(elmLst.size() - 1);

   // verify that the new last friend and last friend from previous list are same
   // if they are same change flag value to false
   // it false the while gets exit
   if (lastElem.equals(nLastElem)) {
    itsNotEnd = false;

  // print names of all friends
  for (WebElement fr : elmLst) {


No comments :

Post a Comment