Friday, 12 September 2014

Login in Facebook with expansion of Friends list of User

Logging into facebook and trying to expansion of friends list by scrolling it down  until the number of friends present for an user.
Example:
package Logs;

import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Print {

public static void main(String args[]) throws IOException, InterruptedException {
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("https://www.facebook.com");

WebDriverWait wait = new WebDriverWait(driver, 3000);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@type='submit']")));

//passing the email address
WebElement userIdTextBox = driver.findElement(By.id("email"));
userIdTextBox.sendKeys(emailaddress);

//passing the password
WebElement pswdTextBox = driver.findElement(By.id("pass"));
pswdTextBox.sendKeys(password);

//click on submit
WebElement submitButton = driver.findElement(By.xpath("//input[@type='submit']"));
submitButton.click();

wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//a[@title='Timeline']")));
WebElement timeLineLink = driver.findElement(By.xpath("//a[@title='Timeline']"));
timeLineLink.click();

wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//a[@data-medley-id='pagelet_timeline_medley_friends']")));
WebElement friendsLink = driver.findElement(By.xpath("//a[@data-medley-id='pagelet_timeline_medley_friends']"));
friendsLink.click();

wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//div[@class='uiProfileBlockContent']//a[contains(@data-hovercard, '/ajax/hovercard/user.php?id=')]")));

// expansion of friendlList
for (int i = 0; i < 15; i++) {
((JavascriptExecutor) driver).executeScript("scroll (0, 1000000)");
Thread.sleep(5000);
}

List<WebElement> friendsNamesList = driver.findElements(By.xpath("//div[@class='uiProfileBlockContent']//a[contains(@data-hovercard, '/ajax/hovercard/user.php?id=')]"));

HSSFWorkbook workBook = new HSSFWorkbook();
HSSFSheet spreadSheet = workBook.createSheet("email");
for (int i = 0; i < friendsNamesList.size(); i++) {
spreadSheet.createRow(i).createCell(0).setCellValue(friendsNamesList.get(i).getText());
}
workBook.write(new FileOutputStream("E:/FB" + new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss").format(new Date()) + ".xls"));

driver.close();
}
}

No comments:

Post a Comment