英文:
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("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());
}
}
}
答案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("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();
}
}
答案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));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论