I'm trying to get an element's text and it somehow isn't working.
## html:
<table class="infobox vcard">
(snip)
<tbody>
<tr>(stuff)</tr>
<tr>(stuff)</tr>
<tr>
<th scope="row" class="infobox-label" style="padding-right: 0.5em;">Website</th>
<td class="infobox-data" style="line-height: 1.35em;">
<a rel="nofollow" class="external text" href="https://group.pingan.com">https://group.pingan.com</a>
</td>
</tr>
<tr>(stuff)</tr>
</tbody>
## code:
website = b.element(class: 'vcard').element(visible: 'Website').following_sibling(index: 1).text
## error:
/Users/rich/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/watir-7.1.0/lib/watir/locators/element/selector_builder.rb:149:in `raise_unless': expected one of [TrueClass, FalseClass], got "Website":String (TypeError)
```
That line with the text "Website" is the only `row` in that `table`, hence why I'm using it. I though using the `.following_sibling` method would flush this out, but it's not usable for some reason and I don't know why. This is on `Wikipedia`.
Anybody know how what I'm doing wrong? How can I fix this?
There are 2 issues:
element(visible: 'Website')
. The :visible locator expects either true
(element can be seen by a person) or false
(element cannot be seen by a person). As you want to locate the cell on text, use the :text locator.following_sibling
, the :index locator is 0-based. Having index: 1
, is looking for the second sibling, which doesn't exist. The cell with the link is index: 0
, which is the default so does not need to be explicitly specified.Putting it together, gives:
browser.element(class: 'vcard').element(text: 'Website').following_sibling.text
=> "https://group.pingan.com"