HOW TO GET TEXT FROM WEB ELEMENT WHICH IS DISPLAYED ON MOUSE OVER?

Here is the very basic program to get text from web element which is displayed on mouse over using Actions. In the below example I going to get text from the homepage of Hello Selenium which comes when performing mouse over on author name.





Java Source code:

package com.helloselenium.selenium.test;

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;

public class getMouseOverText{

 public static void main(String[] args) { 

   WebDriver driver = new FirefoxDriver();

   driver.manage().window().maximize();

   driver.navigate().to("http://helloselenium.blogspot.com");
  
   WebElement link = driver.findElement(By.xpath("//a[@rel='author']"));
  
   new Actions(driver).moveToElement(link).build().perform();
  
   String linkTitle = link.getAttribute("title");
  
   System.out.println(linkTitle);
  }
}

Detailed explanation for the above program is as follows:




Following code is the required packages for Selenium.
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
Following code is the required packages for Actions.
import org.openqa.selenium.interactions.Actions;
Following code is to initialize the Firefox driver.
WebDriver driver = new FirefoxDriver();
Following code is to maximize the Firefox Driver instance.
driver.manage().window().maximize();
Following code is to open the hello selenium blog in browser.
driver.get("http://helloselenium.blogspot.com");
You can also use the following code is to open the hello selenium blog in browser.
driver.navigate().to("http://helloselenium.blogspot.com");


Following code is to store WebElement into a variable.
WebElement link = driver.findElement(By.xpath("//a[@rel='author']"));
Following code is to move the mouse to target element.
new Actions(driver).moveToElement(link).build().perform();
Following code is to get and store the mouse over text.
String linkTitle = link.getAttribute("title");
Following code is to print the stored mouse over text.
System.out.println(linkTitle); 


Post a Comment

0 Comments