簡體   English   中英

列表<webelement>沒有在 selenium 中存儲所有必需的元素</webelement>

[英]List<WebElement> is not storing all the required elements in selenium

我的 myntra 願望清單有 41 種產品,其中 19 種缺貨。 我嘗試打印“缺貨”產品的名稱。

“缺貨”元素有一個常見的 class 名稱,我使用 xpath 通過遍歷父節點和子節點來識別產品名稱。

當我在控制台中驗證它時,它給出了正確的響應。 它顯示了19種產品,當我將鼠標指針懸停時,它按預期突出了缺貨產品。 當我調試代碼時也按預期工作。

但是當我點擊運行時,它只打印了 7 個產品,列表的大小是7

該頁面最初顯示前20 個產品,然后在我們向下滾動時顯示剩余的產品。 在前 20 名中,有7名缺貨。 這可能是一個原因。 如果是這樣,如何處理這個滾動事件?

這是代碼片段:

import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
 
public class stockout {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "path of chromedriver.exe");  
        WebDriver driver = new ChromeDriver();
        WebDriverWait w =new WebDriverWait(driver,30);
        
        driver.get(myntra login page);
    //enter phone number          driver.findElement(By.xpath(("//div[@class='signInContainer']/div[2]/div/input"))).sendKeys(phone number);
        driver.findElement(By.cssSelector("div.submitBottomOption")).click();
        Thread.sleep(2000);
        driver.findElement(By.xpath("//div[@class='bottomeLink']/span")).click();
    //enter password
            driver.findElement(By.xpath("//input[@type='password']")).sendKeys(password);
        driver.findElement(By.cssSelector("button.btn.primary.lg.block.submitButton")).click();
        Thread.sleep(4000);
    //open wishlist
        driver.findElement(By.cssSelector("span.myntraweb-sprite.desktop-iconWishlist.sprites-headerWishlist")).click();
    //add out of stock elements to a list
        List<WebElement> outofstock = driver.findElements(By.xpath("//img[@class='itemcard-outOfStockItemImage itemcard-itemImage']/parent::picture/parent::a/parent::div/parent::div/div[2]/div/p[1]"));
    //explicit wait
            w.until(ExpectedConditions.visibilityOfAllElements(outofstock));
        System.out.println(outofstock.size());
        System.out.println("Items out of stock:");
        for (WebElement product: outofstock)
        {   System.out.println(product.getText());
        }
    }
}

在網上找到了解決方案,但想知道是否有更簡單的方法可以做到這一點。 歡迎提出建議。

我添加了這段代碼向下滾動並且它起作用了:

try {
        Object lastHeight = ((JavascriptExecutor) driver).executeScript("return document.body.scrollHeight");

        while (true) {
            ((JavascriptExecutor) driver).executeScript("window.scrollTo(0, document.body.scrollHeight);");
            Thread.sleep(2000);

            Object newHeight = ((JavascriptExecutor) driver).executeScript("return document.body.scrollHeight");
            if (newHeight.equals(lastHeight)) {
                break;
            }
            lastHeight = newHeight;
        }
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

如果您期望存在特定數量的元素,則可以使用以下代碼。

new WebDriverWait(driver,10).until(ExpectedConditions.numberOfElementsToBe(By by, 19));

暫無
暫無

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

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