谁能告诉我如何使用 selenium webdiver 打印 facebook 中的所有好友列表?

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

谁能告诉我如何使用 selenium webdiver 打印 facebook 中的所有好友列表? 我发现很难加载所有朋友的名字:

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)");

此代码将在 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;
            }

        }
    }

我的博客上有一些帖子打印了所有待处理的好友请求。

代码看起来像这样......

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();
    }
}

请看下面,写所有facebook好友列表的逻辑...

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("sainiwinarth@gmail.com");
        //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());
              }
    }

}