如何通过 Selenium 将鼠标悬停在具有动态 xpath 的不同图像上 Java
How to Mouse Hover over different images with dynamic xpaths through Selenium Java
共有七张图片。我想将鼠标悬停在每个图像上并检查是否显示 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 属性文本。
请试试这个,如果它 works.It 对我有用,请告诉我。
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");
}
到 所有 七 (7) 个图像并检查是否显示文本为 AddToCart 的元素或者你可以使用以下解决方案:
driver.get("http://automationpractice.com/index.php");
((JavascriptExecutor) driver).executeScript("return arguments[0].scrollIntoView(true);", new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[@class='homefeatured']"))));
List<WebElement> myProducts = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//ul[@id='homefeatured']//div[@class='product-image-container']/a/img")));
for(WebElement product:myProducts)
{
new Actions(driver).moveToElement(product).build().perform();
if(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//ul[@id='homefeatured']//div[@class='product-image-container']/a/img//following::a[@title='Add to cart']/span"))).isDisplayed())
System.out.println("AddToCart button is displayed");
else{
System.out.println("AddToCart button is not displayed");
}
}
共有七张图片。我想将鼠标悬停在每个图像上并检查是否显示 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 属性文本。
请试试这个,如果它 works.It 对我有用,请告诉我。
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");
}
到
driver.get("http://automationpractice.com/index.php");
((JavascriptExecutor) driver).executeScript("return arguments[0].scrollIntoView(true);", new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[@class='homefeatured']"))));
List<WebElement> myProducts = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//ul[@id='homefeatured']//div[@class='product-image-container']/a/img")));
for(WebElement product:myProducts)
{
new Actions(driver).moveToElement(product).build().perform();
if(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//ul[@id='homefeatured']//div[@class='product-image-container']/a/img//following::a[@title='Add to cart']/span"))).isDisplayed())
System.out.println("AddToCart button is displayed");
else{
System.out.println("AddToCart button is not displayed");
}
}