Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/367.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Selenium Webdriver和Java通过System.out.println方法仅打印网站的前三条评论_Java_Selenium_Selenium Webdriver_Css Selectors_Webdriver - Fatal编程技术网

如何使用Selenium Webdriver和Java通过System.out.println方法仅打印网站的前三条评论

如何使用Selenium Webdriver和Java通过System.out.println方法仅打印网站的前三条评论,java,selenium,selenium-webdriver,css-selectors,webdriver,Java,Selenium,Selenium Webdriver,Css Selectors,Webdriver,我需要打印网站上的前三篇评论;具体来说,这一点: 这是我的选择器: $$('div.review-content p') 该选择器显示20个可见评论中存在的20个文本。我只需要打印代码中的前三个(0、1、2)文本。不过我还没有办法。总而言之,我的问题是:如果我用我提到的选择器声明了一个包含所有这些元素的列表,那么如何打印该列表中的前三个元素,然后停止?谢谢。在下面的示例中,Xpath已用于定位审阅文本,但是您也可以使用任何其他定位器 List<WebElement> reviewT

我需要打印网站上的前三篇评论;具体来说,这一点:

这是我的选择器:

$$('div.review-content p')

该选择器显示20个可见评论中存在的20个文本。我只需要打印代码中的前三个(0、1、2)文本。不过我还没有办法。总而言之,我的问题是:如果我用我提到的选择器声明了一个包含所有这些元素的列表,那么如何打印该列表中的前三个元素,然后停止?谢谢。

在下面的示例中,Xpath已用于定位审阅文本,但是您也可以使用任何其他定位器

List<WebElement> reviewTexts = driver.findElements(By.xpath("//div[@class='review-content']//p"));
        int i=1;
        for (WebElement reviewText : reviewTexts) {

            System.out.println("Review text " + i + ": " + reviewText.getText());
            i++;

            if (i>3) {
                break;
            }

        }
List reviewTexts=driver.findElements(By.xpath(“//div[@class='review-content']//p”);
int i=1;
对于(WebElement reviewText:reviewText){
System.out.println(“审阅文本”+i+:“+reviewText.getText());
i++;
如果(i>3){
打破
}
}

请尝试上述代码,让我知道它是否适合您。

根据您的问题,仅提取前三篇评论文本,您可以引导WebDriverWait首先创建一个列表并打印所需数量的评论,您可以使用以下解决方案:

  • 代码块:

    System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
    WebDriver driver = new ChromeDriver();
    driver.get("https://www.yelp.com/biz/golden-boy-pizza-san-francisco?osq=Restaurants+Pizza");
    List<WebElement> myList = new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.cssSelector("div.review-content p")));
    for(int i=0; i<3; i++)
        System.out.println(myList.get(i).getAttribute("innerHTML"));
    
    System.setProperty(“webdriver.chrome.driver”,“C:\\Utility\\BrowserDrivers\\chromedriver.exe”);
    WebDriver驱动程序=新的ChromeDriver();
    驱动程序。获取(“https://www.yelp.com/biz/golden-boy-pizza-san-francisco?osq=Restaurants+比萨饼);
    List myList=new WebDriverWait(driver,10).until(预期条件。由.cssSelector(“div.review-content p”))访问的所有组件的可见性);
    
    对于(int i=0;它工作得很好,但在我的圈子里不太受欢迎,因为它在for之前强制条件,他们希望条件在for内停止。不过,谢谢!这让我对如何使用for有了很好的了解。是的!这实际上是我想要的。非常感谢!@FranciscoMaldonado很高兴能够帮助你退出。请点击我的答案旁边的空心记号,它就在votedown箭头的下方,这样记号就会变成绿色。
    When I visited earlier this year, it's more like the pizza visited my very being. It may have taken over a bit, lol. It was and is the best focaccia pizza I have had on the West coast so far, at the very least. Probably the best pizza overall too. There are some close seconds, with different styles, etc of course. Pizza is a very picky, moody and delicate situation. <br><br>I would recommend the garlic slice. Wow. Then the pepperoni. The pesto one was not amazing, but I generally don't like pesto versions of pizza anyway, so thats my pickiness, not so much the place itself.
    Square slice focaccia pizza? Yes please!<br><br>I almost always end up here whenever I'm in the area. Even when I'm not in the area I seem to end up here. My go-to definitely has to be their garlic clam slice, plenty of clams, garlicky goodness, melted cheese, all on top of a fluffy focaccia with a crispy bottom. My boyfriend usually gets the pepperoni or sausage slice and we split a combo slice on top of that, super satisfying. <br><br>Unless you're coming here for late night drunchies, highly recommend coming in the afternoon if you don't want to wait in a long line as it gets pretty crowded at night.
    I ate a slice of combo pizza, which was amazing!<br>I liked how fluffy it was. &nbsp;Limited seating at the restaurant.<br>Bought a slice of Pesto &amp; veggie to go.<br>I was surprised that they did not want to sell me a small box for the slice, even though I requested for it and I agreed to pay the extra 50 cents...weird.