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.

  • 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); – user9391 Nov 29 '14 at 8:53
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();
  • 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 5: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 – JackTurky 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.