关于自动化:无法在Ruby中使用Watir设置表单元素的文本

Cannot set text for form element using Watir in Ruby

在我成功编写了一些代码以使用机械化与笔记本电脑上托管的一个小型网站进行交互之后,我才开始对watir进行修改。但是我正竭尽全力去工作。

1
2
3
4
5
6
7
8
9
require 'watir-webdriver'

b = Watir::Browser.new :firefox
b.goto('https://dev.mitdev.org.uk/login')
body = b.body
body.forms.each do |f|
  p f.name
  f.text_field(:name, 'IDToken1').wait_until_present(5).set('test')
end

我发现打印f.name可行,并且看到登录表单称为"登录"。但是,如果我添加一行以设置文本,则会超时。

看着FF,我可以看到表单已经呈现,所以我对为什么似乎无法与之交互感到困惑。

我尝试了其他设置值的方法,例如:

1
body.text_field(:name => 'IDToken2').wait_until_present(5).set('Password')

但无济于事。

在线教程使这看起来如此简单,以至于我怀疑我只是在做一个菜鸟大错,实在太累了,看不到它。

感谢您的回复。至于例外,除非看到等待和超时,否则我不会看到它。如果有一种强迫它告诉我我的语法有误的方法,我将不胜感激知道该如何做。

以下是形式:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<form id="Login" name="Login" action="/login" method="post">
   
        <fieldset>
            <legend class="form-title heading-large">Sign in</legend>
           
                <label  class="form-label" for="IDToken1">
                    User ID
                </label>
                <input class="form-control" type="text" name="IDToken1" id="IDToken1" value="" maxlength="50" tabindex="1" />
           
           
                <label class="form-label" for="IDToken2">
                    Password
                </label>
                <input type="hidden" id="goto" name="goto" value=""/>
                <input class="form-control" type="password" name="IDToken2" id="IDToken2" value="" maxlength="100" autocomplete="off" tabindex="2" />
                <input type="hidden" name="_csrf_token" value=" deleted">
           
        </fieldset>
        <nav class="module-content-navigation">
            <input name="Login.Submit" type="submit" class="button" value="Sign in" tabindex="3" />
            <ul class="content-navigation__secondary">
               
<li>

                    Forgotten password
               
</li>

           
</ul>

        </nav>
   
    <input name="IDButton" type="hidden" style="display: none;">
</form>

我在网上发现了一个很好的教程,该教程向我展示了如何使用散列进一步限定元素的地址,但这种形式对我仍然没有帮助:

1
2
3
4
5
6
7
b = Watir::Browser.new
b.goto('https://dev.mitdev.org.uk/login')
b.wait(3)
b.text_field(:class => 'form-control',
             :type => 'text',
             :name => 'IDToken1',
             :id => 'IDToken1').set('test')

我仍然收到超时或没有等待的异常:

1
2
3
4
5
[remote server] file:///var/folders/f4/6vwp2t_d75v803226gs84v100000gp/T/webdriver-profile20160612-12708-sjjxdp/extensions/fxdriver@googlecode.com/components/command-processor.js:10092:in `fxdriver.preconditions.visible': Element is not currently visible and so may not be interacted with (Selenium::WebDriver::Error::ElementNotVisibleError)
    from [remote server] file:///var/folders/f4/6vwp2t_d75v803226gs84v100000gp/T/webdriver-profile20160612-12708-sjjxdp/extensions/fxdriver@googlecode.com/components/command-processor.js:12644:in `
DelayedCommand.prototype.checkPreconditions_'
    from [remote server] file:///var/folders/f4/6vwp2t_d75v803226gs84v100000gp/T/webdriver-profile20160612-12708-sjjxdp/extensions/fxdriver@googlecode.com/components/command-processor.js:12661:in `DelayedCommand.prototype.executeInternal_/h'

    from [remote server] file:///var/folders/f4/6vwp2t_d75v803226gs84v100000gp/T/webdriver-profile20160612-12708-sjjxdp/extensions/fxdriver@googlecode.com/components/command-processor.js:12666:in `DelayedCommand.prototype.executeInternal_'
    from [remote server] file:///var/folders/f4/6vwp2t_d75v803226gs84v100000gp/T/webdriver-profile20160612-12708-sjjxdp/extensions/fxdriver@googlecode.com/components/command-processor.js:12608:in `
DelayedCommand.prototype.execute/<'

稍微归零,将输出文本字段的ID:

1
2
3
p b.text_field(:type => 'text',
             :name => 'IDToken1',
             :id => 'IDToken1').id

但这无法设置文本:

1
2
3
b.text_field(:type => 'text',
             :name => 'IDToken1',
             :id => 'IDToken1').when_present(5).set('test')

这看起来越来越像是与页面有关的事情,而不是我对水语法的理解:

1
2
3
4
5
6
7
p b.text_field(:name => /Token1/).name
p b.text_field(:type => 'text',
             :name => 'IDToken1',
             :id => 'IDToken1').id
p b.text_field(:name => /Token1/).exists?
b.text_field(:name => /Token1/).double_click
b.text_field(:name => /Token1/).set 'test'

为我提供了显示元素清楚存在的输出,但是我看到了双击的这个奇怪的"滚动"堆栈跟踪。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
"IDToken1"
"IDToken1"
true
[remote server] file:///var/folders/f4/6vwp2t_d75v803226gs84v100000gp/T/webdriver-profile20160612-2812-w3soqr/extensions/fxdriver@googlecode.com/components/driver-component.js:11072:in `FirefoxDriver.prototype.mouseMoveTo': Offset within element cannot be scrolled into view: (5, 17.5): [object HTMLInputElement] (Selenium::WebDriver::Error::MoveTargetOutOfBoundsError)
    from [remote server] file:///var/folders/f4/6vwp2t_d75v803226gs84v100000gp/T/webdriver-profile20160612-2812-w3soqr/extensions/fxdriver@googlecode.com/components/command-processor.js:12661:in `
DelayedCommand.prototype.executeInternal_/h'
    from [remote server] file:///var/folders/f4/6vwp2t_d75v803226gs84v100000gp/T/webdriver-profile20160612-2812-w3soqr/extensions/fxdriver@googlecode.com/components/command-processor.js:12666:in `DelayedCommand.prototype.executeInternal_'

    from [remote server] file:///var/folders/f4/6vwp2t_d75v803226gs84v100000gp/T/webdriver-profile20160612-2812-w3soqr/extensions/fxdriver@googlecode.com/components/command-processor.js:12608:in `DelayedCommand.prototype.execute/<'
    from /Users/atkinsb/.rvm/gems/ruby-2.2.1/gems/selenium-webdriver-2.53.1/lib/selenium/webdriver/remote/response.rb:70:in `
assert_ok'
    from /Users/atkinsb/.rvm/gems/ruby-2.2.1/gems/selenium-webdriver-2.53.1/lib/selenium/webdriver/remote/response.rb:34:in `initialize'

    from /Users/atkinsb/.rvm/gems/ruby-2.2.1/gems/selenium-webdriver-2.53.1/lib/selenium/webdriver/remote/http/common.rb:78:in `new'
    from /Users/atkinsb/.rvm/gems/ruby-2.2.1/gems/selenium-webdriver-2.53.1/lib/selenium/webdriver/remote/http/common.rb:78:in `
create_response'
    from /Users/atkinsb/.rvm/gems/ruby-2.2.1/gems/selenium-webdriver-2.53.1/lib/selenium/webdriver/remote/http/default.rb:90:in `request'

    from /Users/atkinsb/.rvm/gems/ruby-2.2.1/gems/selenium-webdriver-2.53.1/lib/selenium/webdriver/remote/http/common.rb:59:in `call'
    from /Users/atkinsb/.rvm/gems/ruby-2.2.1/gems/selenium-webdriver-2.53.1/lib/selenium/webdriver/remote/bridge.rb:649:in `
raw_execute'
    from /Users/atkinsb/.rvm/gems/ruby-2.2.1/gems/selenium-webdriver-2.53.1/lib/selenium/webdriver/remote/bridge.rb:627:in `execute'

    from /Users/atkinsb/.rvm/gems/ruby-2.2.1/gems/selenium-webdriver-2.53.1/lib/selenium/webdriver/remote/bridge.rb:419:in `mouseMoveTo'
    from /Users/atkinsb/.rvm/gems/ruby-2.2.1/gems/selenium-webdriver-2.53.1/lib/selenium/webdriver/common/mouse.rb:71:in `
move_to'
    from /Users/atkinsb/.rvm/gems/ruby-2.2.1/gems/selenium-webdriver-2.53.1/lib/selenium/webdriver/common/mouse.rb:81:in `move_if_needed'

    from /Users/atkinsb/.rvm/gems/ruby-2.2.1/gems/selenium-webdriver-2.53.1/lib/selenium/webdriver/common/mouse.rb:40:in `double_click'
    from /Users/atkinsb/.rvm/gems/ruby-2.2.1/gems/selenium-webdriver-2.53.1/lib/selenium/webdriver/common/action_builder.rb:363:in `
block in perform'
    from /Users/atkinsb/.rvm/gems/ruby-2.2.1/gems/selenium-webdriver-2.53.1/lib/selenium/webdriver/common/action_builder.rb:362:in `each'

    from /Users/atkinsb/.rvm/gems/ruby-2.2.1/gems/selenium-webdriver-2.53.1/lib/selenium/webdriver/common/action_builder.rb:362:in `perform'
    from /Users/atkinsb/.rvm/gems/ruby-2.2.1/gems/watir-webdriver-0.9.1/lib/watir-webdriver/elements/element.rb:147:in `
block in double_click'
    from /Users/atkinsb/.rvm/gems/ruby-2.2.1/gems/watir-webdriver-0.9.1/lib/watir-webdriver/elements/element.rb:598:in `element_call'

    from /Users/atkinsb/.rvm/gems/ruby-2.2.1/gems/watir-webdriver-0.9.1/lib/watir-webdriver/elements/element.rb:147:in `double_click'

感觉我越来越接近这个问题:

1
2
3
p b.text_field(:name => /Token1/).exists?
p b.text_field(:id => /Token1/).visible?
p b.text_field(:name => /Token1/).present?

对于可见和存在,此显示为false;对于存在,此显示为true。

为什么这个元素对watir不可见,而对我而言却是我必须回答的下一个问题...


我已经尝试过您的脚本了。它在我的尽头

1
2
3
4
5
require 'watir-webdriver'

b = Watir::Browser.new :chrome
b.goto 'file:///D:/WORKSPACE/Learning/demo1.html'
b.text_field(:name, 'IDToken1').when_present(5).set('test')

但不适用于形式和主体。


1
2
p b.text_field(:name => /Token1/).exists?
p b.text_field(:id => /Token1/).visible?

这两行帮助了我。谢谢。


呜!终于...

因此Selenium Java代码为我提供了正确的方向,并且我设法填写了这样的表格:

1
2
3
b.element(:xpath =>"//*[contains(@id,'_tid1')]").send_keys('user')    
b.element(:xpath =>"//*[contains(@id,'_tid2')]").send_keys('password')
b.element(:name =>"Login.Submit").click