facebookfriendsoffriends.FacebookFriendsOfFriends.java Source code

Java tutorial

Introduction

Here is the source code for facebookfriendsoffriends.FacebookFriendsOfFriends.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package facebookfriendsoffriends;

/**
 *
 * @author cbrom
 */
import java.io.File;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.junit.Test;
import org.openqa.selenium.Alert;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.remote.DesiredCapabilities;

public class FacebookFriendsOfFriends {

    private String userName;
    private String password;
    private String facebookUrl;
    private WebDriver driver;
    private ChromeOptions options;

    /**
     * @param args the command line arguments
     */
    public FacebookFriendsOfFriends(String userName, String password) {
        this.userName = userName;
        this.password = password;
        this.facebookUrl = "https://www.facebook.com";

        this.options = new ChromeOptions();
        options.addArguments("--start-maximized");
        options.addArguments("--disable-web-security");
        options.addArguments("--no-proxy-server");
        Map<String, Object> prefs = new HashMap<String, Object>();
        prefs.put("credentials_enable_service", false);
        prefs.put("profile.password_manager_enabled", false);
        options.setExperimentalOption("prefs", prefs);
        options.addExtensions(new File(
                "/home/cbrom/.config/google-chrome/Default/Extensions/mjnbclmflcpookeapghfhapeffmpodij/1.3.0_0.crx"));
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability(ChromeOptions.CAPABILITY, options);
        this.driver = new ChromeDriver(capabilities);
    }

    public static void main(String[] args) {
        // TODO code application logic here
        FacebookFriendsOfFriends fb = new FacebookFriendsOfFriends("kb.kbrom@gmail.com", "Rkgatar6Lv");
        fb.login();
        fb.friendsOfFriends();
    }

    public void login() {
        this.driver.get(this.facebookUrl);
        try {
            Thread.sleep(1000);
        } catch (Exception e) {

        }
        this.driver.navigate().refresh();

        WebElement userName = driver.findElement(By.name("email"));
        WebElement password = driver.findElement(By.name("pass"));

        //        try{
        //            Thread.sleep(2000);
        //        }
        //        catch(Exception e){
        //            
        //        }

        userName.sendKeys(this.userName);
        password.sendKeys(this.password);
        password.submit();
        try {
            Thread.sleep(2000);
        } catch (Exception ex) {

        }

        List<WebElement> buttons = driver.findElements(By.tagName("a"));
        WebElement not_now = buttons.get(buttons.size() - 2);
        System.out.println("found " + not_now.getText());
        not_now.click();

    }

    public ArrayList<String> userFriends() {
        ArrayList<String> userFriends = new ArrayList<String>();
        WebElement profile = driver.findElement(By.xpath("//a[@title='Profile']"));
        //        driver.findElement(By.xpath("//*[@title='Profile'")).click();
        profile.click();
        try {
            Thread.sleep(1000);
        } catch (Exception ex) {

        }
        userFriends = findFriends(driver.getCurrentUrl());

        for (String link : userFriends) {
            System.out.println("button text=" + link);
        }
        return userFriends;
    }

    public void Scroll_Page_Down() throws Exception {
        try {
            Thread.sleep(1000);
            JavascriptExecutor jse = (JavascriptExecutor) this.driver;
            jse.executeScript("window.scrollBy(0,10000)", "");
        } catch (Exception e) {
        }

    }

    public ArrayList<ArrayList<String>> friendsOfFriends() {
        ArrayList<String> userFriends = this.userFriends();
        ArrayList<ArrayList<String>> friends = new ArrayList<ArrayList<String>>();
        String friend = userFriends.get(15);
        FindFriends x = new FindFriends(this.driver, friend);
        Thread y = new Thread(x);
        y.start();
        //        for (String friend: userFriends){
        //            //new thread
        //            FindFriends x = new FindFriends(driver, friend);
        //            Thread y = new Thread(x);
        //            y.start();
        //            //friends.add(findFriends(friend));
        //        }
        return friends;
    }

    public ArrayList<String> findFriends(String friendLink) {
        this.driver.get(friendLink);
        ArrayList<String> friends = new ArrayList<String>();
        WebElement friendsLink = driver.findElement(By.xpath("//a[@data-tab-key='friends']"));
        friendsLink.click();
        //scroll down
        try {
            this.Scroll_Page_Down();
            this.Scroll_Page_Down();
            this.Scroll_Page_Down();
            this.Scroll_Page_Down();
            this.Scroll_Page_Down();
        } catch (Exception e) {
        }

        WebElement friendsDiv = driver.findElement(By.id("collection_wrapper_2356318349"));
        List<WebElement> friendDivs = friendsDiv.findElements(
                By.xpath("//ul[@data-pnref='friends']//li[@class='_698']//div//div//div//div/a[@class='_39g5']"));
        for (WebElement friendDiv : friendDivs) {
            if (friendDiv.getText().endsWith("mutual friends")) {
                continue;
            }
            friends.add(friendDiv.getAttribute("href"));
        }
        //        WebElement friendsDiv = driver.findElement(By.id("collection_wrapper_2356318349"));
        //        List<WebElement> friend_links = friendsDiv.findElements(By.xpath("//div[@class='fsl fwb fcb']//a"));
        //        for (WebElement link : friend_links) {
        //            friends.add(link.getAttribute("href"));
        //            //System.out.println("button text=" + link.getAttribute("href"));
        //        }
        return friends;
    }
}