How to get the tooltip text using Selenium WebDriver?

1.9k Views Asked by At

I'm trying to get the tooltip text on the search results in www.bigbasket.com and have used the below code

@FindAll({@FindBy(xpath="//*[contains(@id,'product')][not(contains(@style,'display:none'))]/div/span[2]/a")})
    List<WebElement> lblProductName;

public String verifySearchResults(WebDriver browser, String productName){
        System.out.println(lblProductName.size());
        try{
            Actions actions = new Actions(browser);
            for(WebElement eachElement:lblProductName){
                System.out.println(eachElement.getAttribute("href"));
                actions.moveToElement(eachElement).perform();
                //Actions action = new Actions(driver);
                actions.moveToElement(eachElement).build().perform();
                WebElement toolTipElement = browser.findElement(By.cssSelector(".uiv2-tool-tip-hover "));
                System.out.println("Tooltip text is "+toolTipElement.getAttribute("textContent"));
            }
        }catch(Exception e){
            System.out.println(e.getMessage());
        }
        return productName; 
    }

But with the above code I'm able to get only the tool tip text of the first search result. Could you please help me in how to get the tool tip text of all search results?

Manual steps to follow 1. Go to www.bigbasket.com 2. Click Skip and Explore button 3. Search Apple 4. Mouse over each of the search result and view the tool tip text

1

There are 1 best solutions below

0
Naman On

One thing I could observe is there are multiple tool-tip-hover elements where in you are using :

WebElement toolTipElement = browser.findElement(By.cssSelector(".uiv2-tool-tip-hover ")); //this shall always access the same element

Instead you shall either use this as

List<WebElement> toolTipElement = browser.findElements(By.cssSelector(".uiv2-tool-tip-hover "))

OR

I would suggest creating a parent and further accessing its child (you can put it in the code flow accordingly) :

WebElement parentImgTitle = driver.findElements(By.cssSelector(".uiv2-list-box-img-title");
WebElement childTooltip = parentImgTitle.findElement(By.cssSelector(".uiv2-tool-tip-hover "));

Note : Also I am not sure what role your actions shall be playing here. I doubt there presence to get the details over hovering. Just try once to ignore them.