watir/watir_meta

Unable to access the text value of a div id elelment

ShettyA opened this issue · 5 comments

I have a html page with following DOM structure.

<div class="test_123 hide">
  <div id="tab1">tab1, val1, val2</div>
  <div id="tab2">tab2, val1, val2</div>
  <div id="tab3">tab3, val1, val2</div>
  <div id="tab4">tab4, val1, val2</div>
</div>

Though I can detect the sub div element with "id", I cannot read their text. Is there a way I can access it via Watir please? Below are some examples of what I tried and their response.

@browser.div(:class, "test_123 hide").div(:id, "tab1").exists?
true

@browser.div(:class, "test_123 hide").div(:id, "tab1").text
""

@browser.div(:class, "test_123 hide").div(:id, "tab1").value
""

We can look into this but used HTML code is necessary.

Is the div visible to users? Given that there is a "hide" class, I would probably guess it is not. The text method only returns visible text, which is why you get an empty string.

If I recall correctly, you are using Watir-Classic. You can get the non-visible text by doing:

@browser.div(:class, "test_123 hide").div(:id, "tab1").document.innerText

Note: For support type questions, you might want to consider using StackOverflow or the Watir mailing list. You are more likely to get a quicker response there since they have a higher volume of traffic.

Thanks Justin.

I am unable to access 'document' within my script and I get the following:
NoMethodError: undefined method document' for #<Watir::Div:0x007fedcb113940> My Gemfile has the following and no 'watir-classic'. gem "json" gem "json-schema" gem "watir" gem "flags" gem "scope" gem "minitest", "~> 4.5.0" gem "selenium-webdriver", "~> 2.44.0" gem "watir-webdriver", "~> 0.6.11" gem "rest_client" gem "watir-webdriver-performance" I even tried installing watir-classic with following additional gems but that did not load due to some 'Make sure thatgem install win32-api -v '1.4.8'` succeeds before bundling.' issues.
gem "ffi", "> 1.0"
gem "multi_json", ">= 0"
gem "nokogiri", ">= 1.5.7.rc3"
gem "rautomation", "
> 0.7"
gem "win32-process", ">= 0.5.5"
gem "win32screenshot", "~> 1.0.9"
gem "windows-pr", ">= 0.6.6"
gem "watir-classic"

As a workaround, I am trying to get my developer to feed the text data as a attribute value within the div element in following format. I am hoping this makes it accessible.

Sorry, I had assumed you were using Watir-Classic given that you had commented on one of the issues the other day. Since you are using Watir-Webdriver, there were a couple of options on StackOverflow.

Try:

@browser.div(:class, "test_123 hide").div(:id, "tab1").attribute_value('textContent')

Or since there are no children elements, you could also do:

@browser.div(:class, "test_123 hide").div(:id, "tab1").inner_html

Thanks. I got the UI code changed to update values as an attribute value instead of as the text. That got working with following approach.
@browser.div(:class, "test_123 hide").div(:id, "tab1").attribute_value('textContent')