1

Trying to figure out how to select a field on a form using textarea with specifying the class and the ID. Unfortunately, I don't have control over the HTML that is generated as it is a vendor product.

Sample HTML:

<textarea class="text sr " id="arid_WIN_0_636875000" style="left: 402px; top: 0px; width: 261px; height: 21px;" rows="1" cols="20" wrap="off" arautoctt="400" arautocak="0" maxlen="255"></textarea>

I need to use both the ID and the class because the ID is used in multiple spots as well as the class, however, I have found the combination of the two is unique.

I've tried doing:

z = textarea(:class => "text sr ", :id  => "arid_WIN_0_636875000")

This came back with:

=> #<Watir::TextArea:0x272de17c located=false sel
>"arid_WIN_0_636875000", :tag_name=>"textarea"}>

Tried setting it and came back with:

Watir::Exception::UnknownObjectException: unable to locate element, using {:class=>"text sr ", :id=>"arid_WIN_0_636875000", :tag_name=>"textarea"}
from C:/Ruby21/lib/ruby/gems/2.1.0/gems/watir-webdriver-0.7.0/lib/watir-webdriver/elements/element.rb:507:in `assert_exists'
from C:/Ruby21/lib/ruby/gems/2.1.0/gems/watir-webdriver-0.7.0/lib/watir-webdriver/user_editable.rb:32:in `clear'
from C:/Ruby21/lib/ruby/gems/2.1.0/gems/watir-webdriver-0.7.0/lib/watir-webdriver/user_editable.rb:11:in `set'
from (irb):21:in `block in irb_binding'
from C:/Ruby21/lib/ruby/gems/2.1.0/gems/selenium-webdriver-2.45.0/lib/selenium/webdriver/common/target_locator.rb:50:in `window'
from C:/Ruby21/lib/ruby/gems/2.1.0/gems/watir-webdriver-0.7.0/lib/watir-webdriver/window.rb:193:in `use'
from (irb):20
from C:/Ruby21/bin/irb:11:in `<main>'

This is what the HTML is for the popup:

<div class="DIVPopup shadow " id="popup_0" style="left: 495px; top: 161.5px; width: 930px; height: 673px; overflow: hidden; visibility: inherit; z-index: 100004;" arwindowid="0"><table class="DIVPopup ResizablePopup" id="DivTable" cellspacing="0" cellpadding="0"><tbody><tr class="DIVPopupTitleBar DIVDraggable"><td class="topleft">&nbsp;</td><td align="left" class="center" id="title_0" nowrap=""><div class="title">Name of form is here&nbsp;&nbsp;&nbsp;</div><button title="Close" class="Close  right" id="ardivpcl" onclick="var target=FindClickedPopup('0', event); if(target){ target.OnCancel();target.stopBubbling(event);return false;}" type="button"></button></td><td class="topright">&nbsp;</td></tr><tr><td class="DIVPopupBodyNoBorder DIVPopupBodyBorder" colspan="3"><iframe id="1433419427651P" src="/arsys/forms/vmpit-ermdy006/NameOfFormIsHere/Administrator/?cacheid=fa90df9&amp;format=html" frameborder="0" style="width: 100%; height: 652px;"></iframe></td></tr></tbody></table><b class="rndfooter" style="background-color: rgb(255, 255, 255);"><b class="rnd1" style="background-color: rgb(255, 255, 255);"></b><b class="rnd2" style="background-color: rgb(255, 255, 255);"></b><b class="rnd3"></b></b><div class="ResizeHandle  right" id="handle_0"></div></div>
  • id is supposed to be unique. Since you indicate that it is not unique, you have malformed HTML. http://www.w3.org/TR/html-markup/global-attributes.html#common.attrs.id At the very least you need to squak to your vendor that they are giving you bad HTML. – Jeff Price Jun 03 '15 at 20:38
  • The locator works for me (when the typos are fixed). Are you sure the problem is the locator itself? There are a number of reasons why Watir might not be able to locate the element, the most common being the element is in a frame. For some other possible reasons - [see my blog](https://jkotests.wordpress.com/unable-to-locate-element/) – Justin Ko Jun 04 '15 at 02:38
  • I've thought about bringing it up with the vendor but even if they decide to change it, which I highly doubt they will, I wouldn't get that fix for a long time. I very much doubt they would do it though as it would be a big rewrite on their end. Yes, the locator 100% doesn't work for me. Thanks for the blog, going to look into that now. One thing I neglected to mention in the OP is that the html is inside of a popup/dialog box. Once again due to the way BMC wrote their HTML for this, it was a pain even getting inside of that popup. I resorted to using windows.last.use do – hockeybpr25 Jun 04 '15 at 11:53
  • One thing I neglected to mention in the OP is that the html is inside of a popup/dialog box. I had difficulties even getting inside of that. I initially tried doing window(:id => "popup_0").use do, but it gave me that couldn't find element error. I resorted to using windows.last.use do http://watirwebdriver.com/browser-popups/ I'll add the HTML for that in the OP – hockeybpr25 Jun 04 '15 at 12:11

2 Answers2

2

Given the HTML for the popup, I would guess that it is just a div element within the original page rather than its own browser window. Accessing the popup should be with:

browser.div(:id => "popup_0")

That said, I do not see the textarea in it, which means it is likely in the iframe element. Iframes have to explicitly told to Watir, which means to find the textarea you need:

browser.div(:id => "popup_0").iframe.textarea(:class => "text sr ", :id  => "arid_WIN_0_636875000")
Justin Ko
  • 46,526
  • 5
  • 91
  • 101
  • Yep, you're right. I realized earlier it was in an iframe and have been trying to figure out how to access it. This works. – hockeybpr25 Jun 04 '15 at 14:46
0

I think the problem may be that you have a bad call. BTW Don't mix your hash styles. Pick the more modern style over the hashrocket. Try this...

get_a_better_name = textarea(class: "text sr ", id: "arid_WIN_0_636875000")

tip - Using a code linter like rubocop (https://github.com/bbatsov/rubocop) can help catch these errors.

Jeff Price
  • 3,229
  • 22
  • 24
  • Yeah that was just a typo on my part. Everything that I found on the web for this was using hashrockets. I'll change the code to that way now. – hockeybpr25 Jun 04 '15 at 11:57