如何通过SeleniumJava使用动态XPath将鼠标悬停在不同的图像上
共有七幅图片。我想做鼠标悬停在每个图像上,并检查是否显示AddToCart按钮。我尝试了以下代码,但它不起作用 参考:如何通过SeleniumJava使用动态XPath将鼠标悬停在不同的图像上,java,selenium,selenium-webdriver,mousehover,webdriverwait,Java,Selenium,Selenium Webdriver,Mousehover,Webdriverwait,共有七幅图片。我想做鼠标悬停在每个图像上,并检查是否显示AddToCart按钮。我尝试了以下代码,但它不起作用 参考: public boolean checkMouseHoveronAllItems(WebDriver驱动程序) { 字符串xpathOfItems=“//[@id='homefected']/li['+index+']/div/div[1]/div/a[1]/img”; 字符串xpathOfAddToCartButtons=“//div[@class='button-conta
public boolean checkMouseHoveronAllItems(WebDriver驱动程序)
{
字符串xpathOfItems=“//[@id='homefected']/li['+index+']/div/div[1]/div/a[1]/img”;
字符串xpathOfAddToCartButtons=“//div[@class='button-container']/a[@title='Add to cart']”;
布尔res=false;
对于(int index=1;index请尝试一下,并让我知道它是否有效。它对我有效
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");
}
List elmntimg=driver.findElements(By.xpath(“//img[@class='replace-2x img responsive']);
布尔res=false;
对于(int ix=0;ix将鼠标悬停在所有七(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");
}
}
driver.get(“http://automationpractice.com/index.php");
((JavascriptExecutor)driver).executeScript(“返回参数[0].scrollIntoView(true);”,新WebDriverWait(驱动程序,20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath(//a[@class='homefeatured']);
List myProducts=new WebDriverWait(driver,20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath(//ul[@id='homefected']//div[@class='product-image-container']]/a/img));
for(WebElement产品:myProducts)
{
新操作(驱动程序).moveToElement(产品).build().perform();
if(new WebDriverWait(driver,20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath)(//ul[@id='homefected']//div[@class='product-image-container']]/a/img//following::a[@title='Add to cart']/span”)).isDisplayed())
System.out.println(“显示AddToCart按钮”);
否则{
System.out.println(“不显示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");
}
}