簡體   English   中英

Java:如何在滾動和打印文本到頁面末尾時打破 while 循環

[英]Java: How to break the while loop in scrolling and printing the text till end of the page

在滾動和打印文本直到頁面末尾時,我無法打破 while 循環。 請幫助我如何打破循環?

while(true) { 
        //js executor to scroll the page
        JavascriptExecutor js = ((JavascriptExecutor) driver);
        js.executeScript("window.scrollTo(0,document.body.scrollHeight)");
        List<WebElement> restNameList = 
        driver.findElements(By.xpath("//div[@class='nA6kb']"));
        for(WebElement e1 : restNameList) {
        //printing the text
        System.out.println(e1.getText()); 
         } 

馬虎,但這應該可以解決問題。 我不確定你想用什么條件來打破循環,但這里是非常通用的偽代碼。 您可以在 for 循環之前設置一個標志。 例如:

while(true):

     flag = 0

     for (condition):

          if (condition2): 
               flag = 1
               break
          print("xyz")
     if flag = 1:
          break

嘗試這個

bool flag = true;
long lastHeight = 0;
long newHight = 0;
while (flag) {
    //js executor to scroll the page
    JavascriptExecutor js = ((JavascriptExecutor) driver);
    lastHeight = (long) js.executeScript("return document.body.scrollHeight");
    js.executeScript("window.scrollTo(0,document.body.scrollHeight)");
    // Do some waiting

    newHight = (long) js.executeScript("return document.body.scrollHeight");
    if (lastHeight == newHight) {
        flag = false;
    }
    
    lastHeight = newHight;
}

List < WebElement > restNameList =
    driver.findElements(By.xpath("//div[@class='nA6kb']"));
for (WebElement e1: restNameList) {
    //printing the text
    System.out.println(e1.getText());
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM