1

There are seven images. I want to do mouse hover on every image and and check AddToCart button is displayed or not. I have tried following code and it is not working.

Reference: 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;
}

Code always taking the fist element and printing the alt attribute text.

2 Answers 2

3

Please try this and let me know if it works.It worked for me.

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");


            }
Sign up to request clarification or add additional context in comments.

2 Comments

This answer is perfect. If i want to perform same operation to single element also this would work.
How do you avoid the rest of the elements that are found with the xPath of the list of images? Because in the page are displayed 7 elements, but if you paste the XPath in the Web console, it returns 14 elements. In code, it throws an error because the rest of the elements are not displayed.
0

To Mouse Hover over all the seven (7) images and check if the element with text as AddToCart is displayed or not you can use the following solution:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.