Search code examples
watir

In Google translate page, printing the text area is not working


I have written the below watir code.

b.textarea.set text

It's configuring the text within the textarea, but when I attempt to print it, nothing appears. This is because the textarea node itself doesn't contain any text; however, one of the div elements beneath this textarea does. Even when I try to print that specific division using the XPath to locate it, it still doesn't display the expected text. This XPath is used because this particular division is where the text is being set.

//textarea/following-sibling::div[2]/div

But when I try to print it, it's not working either.


Solution

  • To get the text of a textarea, use the #value method:

    b.goto('translate.google.com')
    b.textarea.set 'text'
    b.textarea.value
    #=> "text"
    

    If you want to pull text from the following div, your XPath does work. However, because the element is not visible and #text only returns the visible text, #text will return an empty string. You can use #text_content to return all of the text nodes regardless of visibility:

    b.goto('translate.google.com')
    b.textarea.set 'text'
    b.div(xpath: '//textarea/following-sibling::div[2]/div').text_content
    #=> "text"