Monday, October 10, 2016

How to get or store mouse-over text in selenium webdriver


Think of a scenario where you have a link or text with a mouse over text which gets displayed on mouse over action in your email or a webpage and you need to copy that mouse-over text and save in a variable and print it.

Refer to below piece of HTML code:
<h3><a href="https://www.google.com" title="This is a mouseover text">Google Link</a></h3>

In webpage, the link name is "Google Link" and mouse over text is "This is a mouseover text".

Now task is to copy the mouse over text in a variable using Java in selenium webdriver.

Refer to below selenium webdriver code for solution:
String text1= driver.findElement(By.xpath("//a[text()='Google Link']")).getAttribute("title");
System.out.println(text1);


Printing the value of variable text1 will print "This is a mouseover text" in console. Please remember to verify that your locator path is correct as per your webpage code.


If you still face any issue in running the code, write to me in comments section.