简体   繁体   English

通过使用类别和ID与Watir-WebDriver一起选择textarea

[英]Select textarea by using the class and ID with Watir-WebDriver

Trying to figure out how to select a field on a form using textarea with specifying the class and the ID. 试图弄清楚如何使用textarea通过指定类和ID来选择表单上的字段。 Unfortunately, I don't have control over the HTML that is generated as it is a vendor product. 不幸的是,由于它是供应商产品,因此我无法控制所生成的HTML。

Sample HTML: 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. 我需要同时使用ID和类,因为ID和类都用于多个位置,但是,我发现两者的组合是唯一的。

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: 这是弹出窗口的HTML:

<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>

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. 给定弹出窗口的HTML,我猜想它只是原始页面中的div元素,而不是其自己的浏览器窗口。 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. 就是说,我在其中看不到文本区域,这意味着它可能在iframe元素中。 Iframes have to explicitly told to Watir, which means to find the textarea you need: iframe必须明确告知Watir,这意味着找到所需的文本区域:

browser.div(:id => "popup_0").iframe.textarea(:class => "text sr ", :id  => "arid_WIN_0_636875000")

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. 提示-使用诸如rubocop( https://github.com/bbatsov/rubocop )的代码linter可以帮助捕获这些错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM