How to print online friends name in FB using selenium webdriver?

1.3k views Asked by At

I wrote the below code to check if it is storing the list from chatbox.PlzAdd necessary code to it to do the same. Thnx in advance.

WebDriver driver = new FirefoxDriver();

driver.get("https://www.fb.com");

driver.manage().window().maximize();

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


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

driver.findElement(By.xpath(".//*[@type='submit']")).click();

WebDriverWait wait = new WebDriverWait(driver, 60);
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//a[@title='Profile']")));

Thread.sleep(10000);

//This is path for the friends in the chat box, but its saying not able to find element
List<WebElement> friendsOnChatBox = (List<WebElement>) driver.findElement(By.xpath("//li[@class = '_42fz']"));

System.out.println("The No Of Friends in chatbox is: "+ friendsOnChatBox.size());
1

There are 1 answers

3
Madhan On

There is bug in your code Idk how you have executed it.There will be a ClassCastException thrown at runtime for the following as you are casting WebElement to List<WebElement>.It should be findElements not findElement

 //This is path for the friends in the chat box, but its saying not able to find element
List<WebElement> friendsOnChatBox = (List<WebElement>) driver.findElement(By.xpath("//li[@class = '_42fz']"));

Any how I've altered the xpath to be more specific and this will do the job neatly

   List<WebElement> friendsOnChatBox =  driver.findElements(By.xpath("//li[@class = '_42fz']//div[@class = '_55lr']"));

    System.out.println("The No Of Friends in chatbox is: " + friendsOnChatBox.size());

    for (WebElement friends : friendsOnChatBox) {
        System.out.println(friends.getText());
    }