Facebook

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
  driver.manage().window().maximize();
  // specify page load timeout
  driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
  // specify implicit wait
  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
  // navigate to facebook
  driver.get("https://facebook.com");

  // 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();
   */
  Thread.sleep(1000);
  // wait for some unique element in fb home window
  Wait w = new WebDriverWait(driver, 20);
  w.until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@name='mercurymessages']")));

  // click on user id
  driver.findElement(By.xpath("//div[@id='userNav']")).click();
  // click on friends tab
  driver.findElement(By.xpath("//div[@id='fbTimelineHeadline']//a[@data-tab-key='friends']")).click();

  // get parent element of friends table
  WebElement elmFriendsList = driver
    .findElement(By.xpath("//div[@id='timeline-medley']//ul[@data-pnref='friends']"));

  // 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
   Thread.sleep(2000);

   // 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) {
   System.out.println(fr.getText());
  }
 }

}

No comments :

Post a Comment