List<WebElement>在Selenium中未存储所有所需的元素。

huangapple go评论90阅读模式
英文:

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

问题

我的Myntra心愿单有41件商品,其中19件已经缺货。我尝试打印“缺货”的产品名称。

'缺货' 元素有一个共同的类名,我使用这个类名通过遍历父节点和子节点的方式来识别产品的名称。

当我在控制台中验证时,它给出了正确的响应。它显示了 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", "chromedriver.exe的路径");    
        WebDriver driver = new ChromeDriver();
        WebDriverWait w =new WebDriverWait(driver,30);
        
        driver.get("Myntra登录页面的URL");
        //输入手机号码
        driver.findElement(By.xpath("//div[@class='signInContainer']/div[2]/div/input")).sendKeys("手机号码");
        driver.findElement(By.cssSelector("div.submitBottomOption")).click();
        Thread.sleep(2000);
        driver.findElement(By.xpath("//div[@class='bottomeLink']/span")).click();
        //输入密码
        driver.findElement(By.xpath("//input[@type='password']")).sendKeys("密码");
        driver.findElement(By.cssSelector("button.btn.primary.lg.block.submitButton")).click();
        Thread.sleep(4000);
        //打开心愿单
        driver.findElement(By.cssSelector("span.myntraweb-sprite.desktop-iconWishlist.sprites-headerWishlist")).click();
        //将缺货的元素添加到列表中
        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]"));
        //显式等待
        w.until(ExpectedConditions.visibilityOfAllElements(outofstock));
        System.out.println(outofstock.size());
        System.out.println("缺货商品:");
        for (WebElement product: outofstock)
        {   System.out.println(product.getText());
        }
    }
}

请注意,代码中的 "chromedriver.exe的路径" 部分需要替换为您的Chrome驱动程序的实际路径,"Myntra登录页面的URL" 部分需要替换为Myntra登录页面的实际URL,"手机号码" 和 "密码" 部分需要替换为您的登录凭据。

英文:

My myntra wishlist has 41 products, of which 19 are out of stock. I tried printing the names of the 'out of stock' products.

'out of stock' elements had a common class name using which I identified the product's name using xpath by traversing through parent and child nodes.

when i validated it in console, it gave the right response. It showed 19 products and when i hovered the mouse pointer it highlighted the out of stock products as expected. Works as expected when i debugged the code too.

But when i hit run, it printed only 7 products, size of the list was 7.

The page initially displays top 20 products and later displays the remaining as we scroll down. Out of the top 20, 7 are out of stock. Could this be a reason. If that is the case, how to handle this scroll event?

Here's the code snippet:

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(&quot;webdriver.chrome.driver&quot;, &quot;path of chromedriver.exe&quot;);	
WebDriver driver = new ChromeDriver();
WebDriverWait w =new WebDriverWait(driver,30);
driver.get(myntra login page);
//enter phone number	      driver.findElement(By.xpath((&quot;//div[@class=&#39;signInContainer&#39;]/div[2]/div/input&quot;))).sendKeys(phone number);
driver.findElement(By.cssSelector(&quot;div.submitBottomOption&quot;)).click();
Thread.sleep(2000);
driver.findElement(By.xpath(&quot;//div[@class=&#39;bottomeLink&#39;]/span&quot;)).click();
//enter password
driver.findElement(By.xpath(&quot;//input[@type=&#39;password&#39;]&quot;)).sendKeys(password);
driver.findElement(By.cssSelector(&quot;button.btn.primary.lg.block.submitButton&quot;)).click();
Thread.sleep(4000);
//open wishlist
driver.findElement(By.cssSelector(&quot;span.myntraweb-sprite.desktop-iconWishlist.sprites-headerWishlist&quot;)).click();
//add out of stock elements to a list
List&lt;WebElement&gt; outofstock = driver.findElements(By.xpath(&quot;//img[@class=&#39;itemcard-outOfStockItemImage itemcard-itemImage&#39;]/parent::picture/parent::a/parent::div/parent::div/div[2]/div/p[1]&quot;));
//explicit wait
w.until(ExpectedConditions.visibilityOfAllElements(outofstock));
System.out.println(outofstock.size());
System.out.println(&quot;Items out of stock:&quot;);
for (WebElement product: outofstock)
{	System.out.println(product.getText());
}
}
}

答案1

得分: 1

我在网上找到了解决方案,但想知道是否有更简单的方法来实现这个。欢迎提出建议。

我添加了以下代码段来进行滚动,它有效:

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

如果有更简单的方法,欢迎提出建议。

英文:

Found the solution on the net, but wondering if there's any simpler way to do this. Suggestions are welcomed.

I added this piece of code to scroll down and it worked:

try {
Object lastHeight = ((JavascriptExecutor) driver).executeScript(&quot;return document.body.scrollHeight&quot;);
while (true) {
((JavascriptExecutor) driver).executeScript(&quot;window.scrollTo(0, document.body.scrollHeight);&quot;);
Thread.sleep(2000);
Object newHeight = ((JavascriptExecutor) driver).executeScript(&quot;return document.body.scrollHeight&quot;);
if (newHeight.equals(lastHeight)) {
break;
}
lastHeight = newHeight;
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}

答案2

得分: 1

以下是要翻译的内容:

如果您期望特定数量的元素存在,您可以使用以下代码:

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

英文:

You can use the following code if you are expecting a specific number of elements to be present.

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

huangapple
  • 本文由 发表于 2020年8月7日 03:59:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/63290919.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定