How to get javascript tooltip message in Selenium?

I want to receive a message with a hint that appears when the mouse hovers over the icon. I want to receive a message in the Selenium method gettext()and assign it String.

Below is my code:

   JavascriptExecutor jse = (JavascriptExecutor)driver;
   String script = "return driver.findElement(By.xpath(ObjectRepository.tooltipMsg)).getText();";
   String message = ((JavascriptExecutor) driver).executeScript(script).toString();
   Thread.sleep(3000);
   System.out.println("message "+message);

This does not work and I get an error driver is not defined

+4
source share
2 answers

An error was driver is not definedselected due to the statement below.

return driver.findElement(By.xpath(ObjectRepository.tooltipMsg)).getText();

This is actually not Javascript, but Java code to search for the text of a web element. Therefore, the driver defined in java cannot be used as such when creating javascript.

Using Javascript:

String script = "return document.getElementById("your-id").innerHTML;";   
String message = ((JavascriptExecutor) driver).executeScript(script).toString();

Using Java:

String message = driver.findElement(By.xpath("your XPath")).getText();

, XPath Id . , - .

+2

-, drvier:

WebDriver driver = new FirefoxDriver();

String message = driver.findElement(By.xpath("ObjectRepository.tooltipMsg")).getAttribute("title");
+1

Source: https://habr.com/ru/post/1608614/


All Articles