只读文本字段

发布于 2024-10-11 07:54:51 字数 618 浏览 8 评论 0原文

我的网站中有一个只读文本字段。我需要使用黄瓜和 watir 输入日期。但我无法设置任何值。即使使用 value = 方法传递日期值,我也无法输入任何输出。有一个 JavaScript 日历。

我写道:

browser.text_field(:name => "deal[start_date]").value = 'test'

它显示以下错误:

Watir::Exception::ObjectReadOnlyException: Watir::Exception::ObjectReadOnlyException
    from /var/lib/gems/1.8/gems/watir-webdriver-0.1.7/lib/watir-webdriver/elements/element.rb:252:in `assert_writable'
    from /var/lib/gems/1.8/gems/watir-webdriver-0.1.7/lib/watir-webdriver/elements/text_field.rb:24:in `value='
    from (irb):10

请帮助我,我遇到了麻烦。

I have an read-only text-field in my site. And I need to enter a date using cucumber and watir. But I can't set any value. Even passing the value of date with value = method I can't input any output. There is a JavaScript calendar.

I wrote:

browser.text_field(:name => "deal[start_date]").value = 'test'

it shows the following error:

Watir::Exception::ObjectReadOnlyException: Watir::Exception::ObjectReadOnlyException
    from /var/lib/gems/1.8/gems/watir-webdriver-0.1.7/lib/watir-webdriver/elements/element.rb:252:in `assert_writable'
    from /var/lib/gems/1.8/gems/watir-webdriver-0.1.7/lib/watir-webdriver/elements/text_field.rb:24:in `value='
    from (irb):10

Please help me, I am in a trouble.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

对你的占有欲 2024-10-18 07:54:51

这与 xboxer21 指出的一致。我发现这段代码在一个具有类似日历小部件的网站上对我有用。

假设您有一个 HTML 表单,其输入文本字段设置为只读:

<form name="FindRange" method="post" action="FindRange.asp" onsubmit="return false">
 ...
 <table width="100%" border="0" cellspacing="0" cellpadding="0">
 <tr>
  <td colspan="3" class="FieldLabel">
    <input type="text" name="FromDate" size="16" readonly="true" style="width:85px; background-color:#F8F6E7;">
  </td>
  <td>
     <input type="text" name="ToDate" size="16" readonly="true" style="width:85px; background-color:#F8F6E7;">
  </td>
 </tr>
 ...
</form>

在 Ruby Watir 脚本中使用类似 '@ie.text(:name, "FromDate").set("3/23/2011")' 的代码会导致 Ruby 中出现错误,指出该字段是只读的。但使用 eval() 方法可能允许您使用 Javascript 在幕后设置只读字段。

#Code above these lines instantiate the Watir object in @ie and navigate to the page   #containing the HTML form
#named 'FindRange'
@ie.document.parentWindow.eval("document.FindRange.FromDate.value = '3/23/2011'")
@ie.document.parentWindow.eval("document.FindRange.ToDate.value = '3/24/2011'")

This goes along with what xboxer21 noted. I found this code worked for me on a site that had a similar calendar widget.

Lets say you have an HTML form with input text fields that are set to readonly:

<form name="FindRange" method="post" action="FindRange.asp" onsubmit="return false">
 ...
 <table width="100%" border="0" cellspacing="0" cellpadding="0">
 <tr>
  <td colspan="3" class="FieldLabel">
    <input type="text" name="FromDate" size="16" readonly="true" style="width:85px; background-color:#F8F6E7;">
  </td>
  <td>
     <input type="text" name="ToDate" size="16" readonly="true" style="width:85px; background-color:#F8F6E7;">
  </td>
 </tr>
 ...
</form>

Using code like '@ie.text(:name, "FromDate").set("3/23/2011")' in your Ruby Watir script would result in an error in Ruby stating the field was read-only. But using the eval() method may allow you to use Javascript to set the read-only fields behind the scenes.

#Code above these lines instantiate the Watir object in @ie and navigate to the page   #containing the HTML form
#named 'FindRange'
@ie.document.parentWindow.eval("document.FindRange.FromDate.value = '3/23/2011'")
@ie.document.parentWindow.eval("document.FindRange.ToDate.value = '3/24/2011'")
囚我心虐我身 2024-10-18 07:54:51

好吧,正如错误消息所述,文本字段是只读的。这意味着它无法更改。如果没有 Watir,您将如何更改文本字段的值?您能提供该页面的链接或相关 HTML 吗?

Well, the text field is read only, as the error message says. That means it can not be changed. How would you change the value of the text field without Watir? Can you provide link to the page or relevant HTML?

小清晰的声音 2024-10-18 07:54:51

这就是我在只读文本字段中输入日期的方法,使用的 JS 日历脚本是 http: //www.dynarch.com/projects/calendar/

日期字段旁边有一个小图标,单击它会显示日历小部件。

browser.image(:id,"datewidget-trigger").click # Will display the Calendar 
browser.send_keys("{ENTER}") # Will select current date

如果您想选择未来日期或先前日期

browser.send_keys("{LEFT}")
browser.send_keys("{RIGHT}")

这仅使用 IE 进行了测试。

This is what I did to enter a date in a read only text field, The JS calendar script used was http://www.dynarch.com/projects/calendar/

There is a small icon next to the date field which displays the calendar widget upon clicking it.

browser.image(:id,"datewidget-trigger").click # Will display the Calendar 
browser.send_keys("{ENTER}") # Will select current date

If you want to select a future date or previous date

browser.send_keys("{LEFT}")
browser.send_keys("{RIGHT}")

This has been tested using IE only.

王权女流氓 2024-10-18 07:54:51

尝试执行 JavaScript 本身。

browser.document.parentWindow.execScript("Date_JS_script('date')") 

Try executing the JavaScript itself.

browser.document.parentWindow.execScript("Date_JS_script('date')") 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文