8

非常肯定你必须使用.clear,或者可能不是,因为它似乎对我不起作用,也许我只是错误地实施它我不确定。

例子:

browser.div(:id => "formLib1").clear.type("input", "hi")

谁能告诉我如何简单地清除一个字段然后输入一个新字符串?

4

3 回答 3

11

Assuming we are talking about a text field (ie you are not trying to clear/input a div tag), the .set() and .value= methods automatically clear the text field before inputting the value.

So one of the following would work:

browser.text_field(:id, 'yourid').set('hi')
browser.text_field(:id, 'yourid').value = 'hi'

Note that it is usually preferred to use .set since .value= does not fire events.

于 2012-09-13T13:17:26.697 回答
8

我有一个类似的问题,并且由于某种原因,.set()并且.value=不可用/无法为该元素工作。

元素是 Watir::Input:

browser.input(:id => "formLib1").to_subtype.clear

清除该字段后,我能够输入文本。

browser.input(:id => "formLib1").send_keys "hi"
于 2015-04-10T17:11:54.420 回答
2

我有一个类似的问题,并且由于某种原因,.set()并且.value=不适用于该元素。

该元素是 Watir::HTMLElement:

[2] pry(#<Object>)> field.class
=> Watir::HTMLElement

field.methods.grep /^(set|clear)$/
=> []

我求助于发送退格键,直到该字段的值为""

count = 0
while field.value != "" && count < 50
  field.send_keys(:backspace)
  count += 1
end
field.send_keys "hi"
于 2015-01-23T19:15:19.923 回答