简体   繁体   中英

Selenium CSS Selector get only first element. How to retrieve all the elements

在此处输入图像描述

在此处输入图像描述

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM