10

I'm trying to extract a certain message from my app. The text starts up as visible and then changes to style= "visibility: hidden" the visibility period is rather short, and I was wondering how can I extract this text using Selenium after it changes status to hidden.

I'm using a simple element.getText() command to get this text, which works when it's visible but returns nothing when hidden.

Sam Woods
  • 8,569
  • 24
  • 43
Gil_G
  • 103
  • 1
  • 1
  • 6
  • Thanks a lot . I used in Selenium Webdriver java for getting CheckBox text. My code which worked fine for me: String checkBoxLabel= (String)((JavascriptExecutor)Browser.driver) .executeScript("return arguments[0].innerHTML",checkLabelTextElement); System.out.println("CheckText is"+checkBoxLabel); –  Nov 29 '14 at 08:53

1 Answers1

9

I had the same problem and actually dislike this "feature" in Selenium. In my C# abstraction layer I replaced getText with this:

return ((IJavaScriptExecutor)webDriverInstance).ExecuteScript("return arguments[0].innerHTML", elementInstance).ToString();
alecxe
  • 11,425
  • 10
  • 49
  • 107
Sam Woods
  • 8,569
  • 24
  • 43
  • Thanks a lot for the quick answer, I will give this a try. I'm still kind of new to java and selenium so might take me some time to report if it worked – Gil_G Apr 19 '12 at 16:18
  • Thanks a lot, it worked perfectly, instead of my element.getText(); I used 'JavascriptExecutor js = (JavascriptExecutor) driver; (js.executeScript("return arguments[0].innerHTML", element)); and I get my value regardless of visibility status. – Gil_G Apr 20 '12 at 05:46
  • Oh, yeah i didn't notice that.....Done! – Gil_G Apr 20 '12 at 20:59
  • it extract tag and attribute too.. not only text – Jayyrus Jul 28 '12 at 22:19
  • You are correct. In C# I use this code to remove any tag info so that only inner text is displayed. string returnText = ((IJavaScriptExecutor)WTWebUiAuto.webDriver).ExecuteScript("return arguments[0].innerHTML", this.WtElement).ToString(); returnText = Regex.Replace(returnText, "<.*?>", ""); – Sam Woods Jul 30 '12 at 16:57