Can anyone tell me how to print all the friend list in facebook using selenium webdiver?

4.3k views Asked by At

Can anyone tell me how to print all the friend list in facebook using selenium webdiver? I am finding it difficult to load all the friends names:

driver.findElement(By.xpath(".//*[@id='email']")).sendKeys("*****");

driver.findElement(By.xpath(".//*[@id='pass']")).sendKeys("*********");

driver.findElement(By.xpath(".//*[@value='Log In']")).sendKeys(Keys.ENTER);

WebElement elememt1 = driver.findElement(By.xpath(".//*[@title='Profile']"));

WebDriverWait wait1 = new WebDriverWait(driver, 20);

wait1.until(ExpectedConditions.visibilityOf(elememt1));

elememt1.click();

WebDriverWait wait2 = new WebDriverWait(driver, 20);

wait2.until(ExpectedConditions.visibilityOf(driver.findElement(By.xpath(".//*[@data-tab-key='friends']"))));

driver.findElement(By.xpath(".//*[@data-tab-key='friends']")).click();


((JavascriptExecutor) driver)
    .executeScript("window.scrollTo(0, 1000)");
3

There are 3 answers

2
Chandan Nayak On BEST ANSWER

This code will print all your friend's name on FB:

@Test public void doLogin() throws InterruptedException{

        // Login and navigate to frineds tab
        driver.get(config.getProperty("URL"));
        driver.findElement(By.xpath("//*[@id='email']")).sendKeys(config.getProperty("Login"));
        driver.findElement(By.xpath("//*[@id='pass']")).sendKeys(config.getProperty("Password"));
        driver.findElement(By.xpath("//*[@id='pass']")).sendKeys(Keys.RETURN);
        Thread.sleep(5000);
        driver.findElement(By.xpath("//*[@title='Profile']")).click();

        //find your frineds count
        String frinedsCount = driver.findElement(By.xpath("//*[@data-tab-key='friends']")).getText().substring(7);
        int count = Integer.parseInt(frinedsCount);

        //click on frineds tab
        driver.findElement(By.xpath("//*[@data-tab-key='friends']")).click();

        //find your couurent loaded frineds count and get it in a list
        List<WebElement> frineds = driver.findElements(By.xpath("//*[@class='fsl fwb fcb']"));
        int found = frineds.size();


        while (found <= count){

            //scroll to the last friend found from the current loaded friend list
            Coordinates coordinate = ((Locatable) frineds.get(found-1)).getCoordinates();
            coordinate.onPage();
            coordinate.inViewPort();
            frineds = driver.findElements(By.xpath("//*[@class='fsl fwb fcb']"));
            found = frineds.size();

            // break and print frined list if the condition found frineds = count of frined list
            if (found == count){
                System.out.println(found);
                System.out.println("---Printing FriendList---");
                for (int i=0; i<found; i++){
                System.out.println(frineds.get(i).getText());
                }
                break;
            }

        }
    }
0
Arpan Saini On

Please find below, the logic to write the list of all facebook friend...

package module15.Part4;


import java.io.IOException;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;

public class Facebook_GetFriendList {


    static WebDriver driver = null;
    public static void main(String[] args) throws IOException, InterruptedException {

        driver = new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
        driver.manage().window().maximize();

        //Code to Login in Facebook
        driver.get("http://facebook.com");
        driver.findElement(By.xpath("//*[@id='email']")).sendKeys("[email protected]");
        //Sorry Can't share my facebook password 
        driver.findElement(By.xpath("//*[@id='pass']")).sendKeys("****************");
        driver.findElement(By.xpath("//*[@id='pass']")).sendKeys(Keys.ENTER);

        driver.findElement(By.xpath("//*[@id='pagelet_welcome_box']/ul/li/div/a")).click();
        System.out.println("Clicking on : " + driver.findElement(By.xpath("//div[@id='fbTimelineHeadline']/div[2]/div/div/a[3]")).getText());;
        driver.findElement(By.xpath("//div[@id='fbTimelineHeadline']/div[2]/div/div/a[3]")).click();

        //xpath of all the names link
        ////div[@id='pagelet_timeline_medley_friends']/div[2]/div/ul/li/div/div/div[2]/div/div[2]/div/a

        List<WebElement> listoffriends = driver.findElements(By.xpath("//div[@id='pagelet_timeline_medley_friends']/div[2]/div/ul/li/div/div/div[2]/div/div[2]/div/a"));
             System.out.println(listoffriends.size());

             int newfriends = listoffriends.size();
              int oldfriends = 0;
              Actions act = new Actions(driver);
              while(oldfriends != newfriends){
                  oldfriends = newfriends ;
                  WebElement lastfriend = listoffriends.get(newfriends-1);
                  act.moveToElement(lastfriend).build().perform();
                  Thread.sleep(4000);
                  listoffriends = driver.findElements(By.xpath("//div[@id='pagelet_timeline_medley_friends']/div[2]/div/ul/li/div/div/div[2]/div/div[2]/div/a"));             
                  newfriends = listoffriends.size();
                  System.out.println("New friends list : " + newfriends);
                  System.out.println("Old friends list : " + oldfriends);
              }

              for (int i =0 ; i< newfriends ; i++){

                  System.out.println("Fiend name : " + listoffriends.get(i).getText());
              }
    }

}
0
MCKiran On

There are posts at my blog that prints all your pending Friend requests.

The code looks like this.....

final By SEE_ALL_FRIENDREQUESTS = By.xpath("//div[@class='jewelFooter']/a");
final By SEE_ALL_TOTAL_REQUEST = By.xpath("//div[@class='phl']/div");
final By SEE_ALL_FRIENDNAME = By.xpath("div/div[2]/div/a");

 void findAllPendingFriendRequests(){      
    try {
        List<WebElement> allFriendsNodes = null;

        if(waitForElement(FRIENDREQUEST_ICON, 10))
            driver.findElement(FRIENDREQUEST_ICON).click();

        if(waitForElement(SEE_ALL_FRIENDREQUESTS, 15))
            driver.findElement(SEE_ALL_FRIENDREQUESTS).click();

        if(waitForElement(SEE_ALL_TOTAL_REQUEST, 15))
            allFriendsNodes = driver.findElements(SEE_ALL_TOTAL_REQUEST);

        if(allFriendsNodes != null){
            System.out.println("BELOW ARE THE FRIEND NAMES:");
            System.out.println("--------------------------");
            for(int i=0; i<allFriendsNodes.size(); i++){
                List<WebElement> myFriend = null;
                if((myFriend=allFriendsNodes.get(i).findElements(SEE_ALL_FRIENDNAME)).size()>0){
                    System.out.println(myFriend.get(0).getText());
                }
                else
                    System.out.println("Issue with reading friend name at the index: "+i);
            }
        }
        else
            System.out.println("Issue in See All Friend requests Page, Probably page didnt load in given time");
    } catch (Exception e) {
        e.printStackTrace();
    }
}