In this article, discussing about how to get text which is displayed on the slider hover image. It is a typical situation to get hover text which has animation effect.
- In this example, access web site http://www.hamaracoupons.in/all-categories.
- At the left side there is a gadget called 'Popular stores', and each store which shows how many no.of coupons are available currently.
- Mouse hover on any store image, you can find a sliding hover image contains a text.
- Here, the test scenario is, locate the hover overlay and get the hover text from each store and display them on the console.
First, locate the web element of the gadget 'Popular Stores' section. Then xpath will be html/body/div[4]/div/div/div[2]/div[1].
WebElement container = driver.findElement(By.xpath("html/body/div[4]/div/div/div[2]/div[1]"));
Then listout elements of all the div's under it. Xpath of the one element is .//div[@class='Similar Similar-ninth'].
List<WebElement> allAdds = container.findElements(By.xpath(".//div[@class='Similar Similar-ninth']"));
Now, find the xpath of the any one of the hover overlay image. html/body/div[4]/div/div/div[2]/div[1]/div[2]/div/div/a.
And listout all the elements of the hover overlay image.
List<WebElement> hoverText = driver.findElements(By.xpath("html/body/div[4]/div/div/div[2]/div[1]/div/div/div/a"));
And, Iterate through each element and Mouse over on it using moveElement() method using Actions class. then get the text of each sliding hover image and print it on the console.
Actions action = new Actions(driver);
action.moveToElement(allAdds.get(i)).build().perform();
hoverText.get(i).getText();
SlidingHoverImage.java
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.apache.commons.lang3.text.WordUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import com.sun.org.apache.xerces.internal.util.URI;
import com.sun.org.apache.xerces.internal.util.URI.MalformedURIException;
public class SlidingHoverImage {
public static WebDriver driver = null;
public static void main(String[] args) throws InterruptedException, MalformedURIException {
driver = new FirefoxDriver();
driver.get("http://www.hamaracoupons.in/all-categories");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Actions action = new Actions(driver);
WebElement container = driver.findElement(By.xpath("html/body/div[4]/div/div/div[2]/div[1]"));
List<WebElement> allAdds = container.findElements(By.xpath(".//div[@class='Similar Similar-ninth']"));
System.out.println(allAdds.size());
//Get all tooltip elements
List<WebElement> hoverText = driver.findElements(By.xpath("html/body/div[4]/div/div/div[2]/div[1]/div/div/div/a"));
System.out.println("Hover Text Size:" + hoverText.size());
//Print all advertisement names and their coupons display on the tooltip
for (int i = 0; i < allAdds.size(); i++) {
String name = hoverText.get(i).getAttribute("href");
//Get last part of the url
URI uri = new URI(name);
String[] segments = uri.getPath().split("/");
String partURL = segments[segments.length-1];
//Split a string using separator
String splt[] = partURL.split("-");
//Capitalize first letter in a word
String firstUpper = WordUtils.capitalize(splt[0]);
action.moveToElement(allAdds.get(i)).build().perform();
Thread.sleep(2000);
String text = hoverText.get(i).getText();
System.out.println(firstUpper + ": " + text);
}
driver.close();
}
}