0

enter image description here

enter image description here

Getting only one review using the cssSelector. Need to extract all the element of div.

public void getFacebookData()
{
    driver.get("https://www.facebook.com/?stype=lo&jlou=AffEX_j6PH-bDCZt13fmzFUMnE49egOV7IU-LhSi3jQAof5FGyuyPmrV48JQ-DjXrgm2fzxsvhC5L6NOWURamMZiNxooPxopmjtkYs2fn5yNzg&smuh=49385&lh=Ac9lhVHRV5hDKKho4Fw");
    try {
        Thread.sleep(20000);
        driver.get(FACEBOOKURL);
        final WebElement  totalRatings = driver.findElement(By.className("p8bdhjjv"));
        System.out.println("total ratings "+totalRatings.getText()); 
        final List<WebElement>  facebookReview = (List<WebElement>) driver.findElements(By.cssSelector("div[class='g4tp4svg mfclru0v om3e55n1 p8bdhjjv']"));
        System.out.println("size of facebook review list "+facebookReview.size());
        
        facebookReview.forEach(review -> {
            System.out.print("reviews are: "+review.getText());
            
            
        });
    } catch (InterruptedException e) {
        e.printStackTrace();
    }   
}
    
Urvashi Meena
  • 37
  • 2
  • 9

1 Answers1

0

this should work for you:

driver.findElements(By.xpath("//div[contains(@class, 'g4tp4svg') and contains(@class, 'mfclru0v') and contains(@class, 'om3e55n1') and contains(@class, 'p8bdhjjv')]"))

however, it might be slower than the css selector, since the xpath is (in theory) the slowest one

Marek
  • 438
  • 5
  • 10
  • using this also it only takes first review. Not able to get all the elements having the same class – Urvashi Meena Sep 29 '22 at 13:55
  • 1
    are all elements displayed, when selenium is trying to find them? perhaps your page isn't fully loaded and only 1 is displayed, when you're trying to collect them – Marek Sep 29 '22 at 14:28
  • I checked on the google, as per that, cssSelector only take first element of that. https://stackoverflow.com/questions/71548292/selenium-css-selector-get-only-first-element-while-the-inspector-show-4 For instance I also try:- `final List facebookReview = (List) driver.findElements(By.cssSelector(".g4tp4svg.mfclru0v.om3e55n1.p8bdhjjv'));` – Urvashi Meena Sep 30 '22 at 06:56