gpt4 book ai didi

java - 如何通过 Selenium Java 使用动态 xpath 将鼠标悬停在不同图像上

转载 作者:行者123 更新时间:2023-12-02 01:40:22 26 4
gpt4 key购买 nike

有七张图片。我想将鼠标悬停在每个图像上,然后检查 AddToCart 按钮是否显示。我已经尝试过以下代码,但它不起作用。

引用:http://automationpractice.com/index.php

public boolean checkMouseHoveronAllItems(WebDriver driver)
{
String xpathOfItems="//[@id='homefeatured']/li['+index+']/div/div[1]/div/a[1]/img";
String xpathOfAddToCartButtons="//div[@class='button-container']/a[@title='Add to cart']";
boolean res=false;
for(int index=1;index<=countNoOfItems(driver);index++)
{
element=driver.findElement(By.xpath(xpathOfItems));
performMouseHover(element);
System.out.println("Item By index"+element.getAttribute("alt"));
element=element.findElement(By.xpath(xpathOfAddToCartButtons));
if(element.isDisplayed())
{
res=true;
Log.info("Element is available");
}
else
{
res=false;
}
}
return res;
}

代码始终采用第一个元素并打印 alt 属性文本。

最佳答案

请尝试一下,让我知道它是否有效。它对我有用。

List<WebElement> elmntimg=driver.findElements(By.xpath("//img[@class='replace-2x img-responsive']"));
boolean res=false;

for(int ix=0;ix<elmntimg.size();ix++)
{
Actions action=new Actions(driver);
action.moveToElement(elmntimg.get(ix)).build().perform();
System.out.println("Item By index"+elmntimg.get(ix).getAttribute("alt"));
WebElement elecart=driver.findElement(By.xpath("//div[@class='button-container']/a[@title='Add to cart']"));
if(elecart.isDisplayed())
res=true;
System.out.println("Element is available");


}

关于java - 如何通过 Selenium Java 使用动态 xpath 将鼠标悬停在不同图像上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54526819/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com