How To Get Tooltip Text In Selenium WebDriver | Software Testing Material
How To Get Tooltip Text In Selenium:
Tooltip Text is a message that appears when we mouse hover on an element. Tooltip is also known as infotip or hit.
When an user mouse hover on an element then a tool tip may appear. Sometimes we need to handle the tooltip text while we are doing automation. Here in this post, we will see how to handle tooltip in Selenium WebDriver.
I use Selenium Actions Class to handle this.
Must Read: Mouse Hover Actions in Selenium
Create object of an Actions Class by passing the WebDriver instance. With the object of the Actions class, driver moves to the required element.
WebElement ele = driver.findElement(By.xpath("xpath")); //Create object 'action' of an Actions class Actions action = new Actions(driver); //Mouseover on an element action.moveToElement(ele).perform();
Let’s see a practical example on how to get Tool Tip Text in Selenium WebDriver:
Given clear explanation in the comments section within the program itself. Please go through it to understand the flow.
package softwaretestingmaterial; 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 org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; import org.testng.Assert; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; public class ToolTipTest { @Test public void toolTipMethod() { System.setProperty("webdriver.chrome.driver", "D:\\Selenium Environment\\Drivers\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.manage().window().maximize(); driver.get(https://jqueryui.com/tooltip/); // Explicit wait to wait for the frame. WebDriverWait wait = new WebDriverWait(driver, 5); wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector(".demo-frame"))); WebElement ele = driver.findElement(By.id("age")); // Using actions class to do mouse hover Actions action = new Actions(driver); action.moveToElement(ele).build().perform(); WebElement toolTipEle = driver.findElement(By.xpath("//*[@id='ui-id-118']/div")); // Get the Tool Tip Text String toolTipTxt = toolTipElement.getText(); // Using assert to verify the expected and actual value Assert.assertEquals("We ask for your age only for statistical purposes.", toolTipTxt); } }
Execute the above script and see the console output.
Also we could handle tooltip using getAttribute method. Sometimes we could see title attribute related to tooltip while identifying elements and in those cases, we could handle using getAttribute() method.
String actualTooltipTxt = "tool tip text"; String tooltipTxt = driver.findElement(By.xpath("xpath")).getAttribute("title"); Assert.assertEquals(actualTooltipTxt, tooltipTxt);
If you are not a regular reader of my blog then I highly recommend you to signup for the free email newsletter using the below link.