Java tutorial
/* * 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; import java.util.ArrayList; import java.util.List; import org.openqa.selenium.By; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.interactions.internal.Coordinates; import org.openqa.selenium.internal.Locatable; /** * * @author cbrom */ public class FindFriends implements Runnable { WebDriver driver; String friendUrl; ArrayList<String> friendLinks; public FindFriends(WebDriver driver, String friendUrl) { this.driver = driver; this.friendUrl = friendUrl; } public ArrayList<String[]> getFriends() { this.driver.get(this.friendUrl); ArrayList<String[]> friends = new ArrayList<String[]>(); WebElement friendLength = driver.findElement(By.xpath("//a[@name='All Friends']//span[@class='_3d0']")); System.out.println(friendLength.getText()); String strNumberOfFriends = friendLength.getText(); int numberOfFriends = Integer.valueOf(strNumberOfFriends); WebElement friendsDiv = driver.findElement(By.id("collection_wrapper_2356318349")); //List<WebElement> friendDivs = friendsDiv.findElements(By.xpath("//ul[@data-pnref='friends']//li[@class='_698']")); List<WebElement> friendDivs = driver.findElements(By.xpath("//*[@class='fsl fwb fcb']")); int found = friendDivs.size(); int prev = 0; while (found <= numberOfFriends) { try { this.scrollPageDown(); this.scrollPageDown(); } catch (Exception e) { } friendDivs = driver.findElements(By.xpath("//*[@class='fsl fwb fcb']")); prev = found; found = friendDivs.size(); System.out.println(found + " " + prev); // break and print frined list if the condition found frineds = count of frined list if (found == prev) { System.out.println(found); System.out.println("---Printing FriendList---"); for (int i = 0; i < found; i++) { System.out.println(friendDivs.get(i).getText()); } break; } } friendDivs = friendsDiv.findElements(By.xpath("//ul[@data-pnref='friends']//li[@class='_698']")); System.out.println(friendDivs.size()); for (WebElement friendDiv : friendDivs) { if (friendDiv.getText().endsWith("mutual friends")) { continue; } List<WebElement> nameLinkAttribute = friendDiv.findElements(By.tagName("a")); String[] nameLink = { nameLinkAttribute.get(0).getText(), nameLinkAttribute.get(1).getAttribute("href") }; System.out.println( nameLinkAttribute.get(1).getText() + ":" + nameLinkAttribute.get(1).getAttribute("href")); friends.add(nameLink); } return friends; } public ArrayList<String[]> getFriendInfo(String name, String friendLink) { this.driver.get(friendLink); ArrayList<String[]> friendInfo = new ArrayList<String[]>(); String email = ""; String phone = ""; WebElement friendsLink = driver.findElement(By.xpath("//a[@data-tab-key='friends']")); return friendInfo; } public ArrayList<String> findFriends() { this.driver.get(this.friendUrl); ArrayList<String> friends = new ArrayList<String>(); WebElement friendsLink = driver.findElement(By.xpath("//a[@data-tab-key='friends']")); friendsLink.click(); //scroll down try { this.scrollPageDown(); } catch (Exception ex) { } List<WebElement> friendDivs = driver.findElements(By.className("_698")); for (WebElement friendDiv : friendDivs) { WebElement friendLink = friendDiv.findElement(By.xpath("//a[@class='_39g5']")); //List<WebElement> friendDivs = uls.findElements(By.xpath("//div[@class='fsl fwb fcb']//a")); friends.add(friendLink.getAttribute("href")); System.out.println(friendLink.getText()); //System.out.println("button text=" + link.getAttribute("href")); } this.friendLinks = friends; return friends; } public void scrollPageDown() throws Exception { try { JavascriptExecutor jse = (JavascriptExecutor) this.driver; jse.executeScript("window.scrollBy(0,1000)", ""); Thread.sleep(1000); } catch (Exception e) { } } @Override public void run() { this.getFriends(); } }