Search code examples
htmlrubywatirwatir-webdriverwatir-classic

Access <body> HTML element using watir


Hi I want to compose the mail,the textarea of the mail has the following HTML code

Can anyone help me out how to access it using watir In have done the following steps but couldn't get it

irb
require 'rubygems'
require 'watir'

@ie.body(:id,':158').set 'text'

MY system configuration is

IE-8
Windows-7

    <div class="Ar Au" id=":88" style="display: block;">
    <iframe tabIndex="1" class="Am Al editable" id=":83" src="html/compose/static_files/blank_quirks.html" frameBorder="0" style="padding-bottom: 0px; background-color: white; padding-left: 0px; padding-right: 0px; height: 245px; overflow: visible; padding-top: 0px;" allowTransparency="allowtransparency" closure_lm_423419="null">
<body class="editable LW-avf" id=":158" role="textbox" contentEditable="true"
    hideFocus="hidefocus" closure_lm_478727="[object Object]" g_editable="true"/>

Solution

  • For editable elements that are not inputs, ie the body element in this case, there is no set method. For these types of elements, you need to use the send_keys method. Assuming a current version of Watir, the following will work:

    @ie.iframe(:id => ':83').body(:id, ':158').send_keys('text')
    

    As there should only be one body element in an iframe, you could also do:

    @ie.iframe(:id => ':83').body.send_keys('text')
    

    However, given that you are using Watir v1.6.7, several of these methods are different or do not exist. For example, there is no iframe or body method. As well, there is no send_keys method for elements. Instead you will need to bring focus to the body element and then send_keys to the browser.

    @ie.frame(:id, ':83').element(:id, ':158').focus
    @ie.send_keys('text')
    

    Note that the element needs to be in focus, so do not take focus away from the browser when doing this.