-1

I'm unable to get tooltip text of an image element on Salesforce(CRM application) page. There is a help icon beside a field and when the user hovers over the icon a message is displayed like this-click here for image, and I want to capture the help text and assert it for verification.

Firstly, I cannot use .getAttribute("title") as there no text in the title attribute; secondly, .getText() is Not working when I give XPath to the image and apply .getText().

Here is the page source code snippet  Click here for page source code

Could anyone help me with this, please?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Avinash
  • 1
  • 1

2 Answers2

0

Can you tell me why .getText() is not working? Alternately, you can do this: 1. Extract the HTML code 2. Save it as String 3. Split the string to extract the required text

see How to get HTML code of a WebElement in Selenium

OR

//moving to element which triggers this tooltip
Actions action= new Actions(driver); 
action.moveToElement(driver.findElement(By.xpath("//td[@class='labelcol']/span[@class='helpButtonOn']"))).build().perform();
//insert wait here
String hovertext=driver.findElement(By.xpath("//td[@class='labelcol']/span[@class='helpButtonOn']/script")).getText();
System.out.println(hovertext);

OR

// javascript executor
WebElement element = driver.findElement(By.xpath("//td[@class='labelcol']/span[@class='helpButtonOn']/script"));
String hovertext = (String)((JavascriptExecutor)driver).executeScript("return arguments[0].innerHTML", element);
System.out.println(hovertext);
theGuy
  • 683
  • 5
  • 15
  • I'm getting a null value when I used .getText(); – Avinash Jul 26 '18 at 06:49
  • Below is the output I got when I used assert: "java.lang.AssertionError: expected [Molecular Center of Excellence] but found []" @theguy – Avinash Jul 26 '18 at 06:55
  • sorry, you said "secondly, .getText() is Not working when I give XPath to the image and apply .getText()." But you should apply .getText() to script not img. – theGuy Jul 26 '18 at 14:42
  • Yes, I tried .getText(); but I got an empty text when I used it to script. @theGuy – Avinash Jul 27 '18 at 07:25
  • looks like this tooltip text is hidden. Other things you can try is to first move to the element which triggers this tooltip and then extract text or try javascript executor. I have updated my answer with both options. – theGuy Jul 27 '18 at 19:03
0

Thank you!!! This thing below worked for me; the message was found within script tag attribute "text content" OR "innerHTML" which was hidden. Here is the code:

public void helpTextAssert(WebElement pathOfToolTip, String enterFieldName, String enterExpectedHelpText) { String originalActualText = pathOfToolTip.getAttribute("textContent"); //Used the below boolean variable, since i have got extra text in text content attribute Boolean containsTextFlag = pathOfToolTip.getAttribute("textContent").contains(enterExpectedHelpText); if (originalActualText != null && enterExpectedHelpText != null && originalActualText.length() >= enterExpectedHelpText.length() && containsTextFlag == true) { System.out.println("Help Text: " + enterExpectedHelpText + " is available for field " + enterFieldName); } else { sAssert.assertEquals(containsTextFlag, true, "Help text Assersion failed for field: " + enterFieldName); System.out.println("Help Text: " + enterExpectedHelpText + " is NOT available"); } }

Avinash
  • 1
  • 1