Search code examples
internet-explorerfirefoxautomated-testswatirfirewatir

In Watir, how can I access the text inside a <dd>?


Here's what the HTML looks like:

<dt> Static text. </dt>
<dd>
    <a onclick="bunch of ajax stuff", href="#">
        Dynamic Text!
    </a>
</dd>

I tried to use the <dt> to locate the <dd> element with the code $browser.dd(:after? => $browser.dt(:text => /Static text./)).text, but that gives an undefined method 'join' for #<String:0xblah> error. The dd doesn't have an id or anything to locate it with. I was able to get the .text from it by doing a regex search for part of it in irb, but that won't work too well long-term since it's a dynamic value.


Solution

  • I assume your HTML looks like the following, plusing your above comment. Is that correct?


    <dl>
    <dt> Static text. </dt>
    <dd>
        <a onclick="bunch of ajax stuff", href="#">
            Dynamic Text!
        </a>
    </dd>
    <dd id='foo'>
        bar
    </dd>
    </dl>
    

    If so, you might be able to get the first DD text like this.

    browser.dt(:text,/Static text/).parent.dd(:index,1).text
    

    or

    browser.dt(:text,/Static text/).parent.dds.first.text
    

    If you can grab the second DD easily, the following is worth to try, I think

    browser.dd(:id,'foo').parent.dds.first.text
    

    or

    browser.element(:id,'foo').parent.dds.first.text
    

    or.. If the target DD is always the next sibling of DT, try element_by_xpath. It's my last resort :)

    browser.dt(:text,/Static text/).element_by_xpath('following-sibling::*').text