如何从隐藏的选择列表中选择值?

时间:2014-07-14 10:22:18

标签: ruby selenium phantomjs html-select watir

我想从this website中选择隐藏的选择列表,然后选择0 - 0,5选项。

我不断获得(Selenium::WebDriver::Error::ElementNotVisibleError)

完整的错误日志:

/Users/#/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/selenium-webdriver-2.42.0/lib/selenium/webdriver/remote/response.rb:51:in `assert_ok': {"errorMessage":"Element is not currently visible and may not be manipulated","request":{"headers":{"Accept":"application/json","Accept-Encoding":"gzip;q=1.0,deflate;q=0.6,identity;q=0.3","Connection":"close","Content-Length":"2","Content-Type":"application/x-www-form-urlencoded","Host":"127.0.0.1:8910","User-Agent":"Ruby"},"httpVersion":"1.1","method":"POST","post":"{}","postRaw":"{}","url":"/click","urlParsed":{"anchor":"","query":"","file":"click","directory":"/","path":"/click","relative":"/click","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/click","queryKey":{},"chunks":["click"]},"urlOriginal":"/session/115feff0-0b40-11e4-a69f-e39c52da2b2d/element/%3Awdc%3A1405333055513/click"}} (ReqHand) (Selenium::WebDriver::Error::ElementNotVisibleError)
        from /Users/#/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/selenium-webdriver-2.42.0/lib/selenium/webdriver/remote/response.rb:15:in `initialize'
        from /Users/#/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/selenium-webdriver-2.42.0/lib/selenium/webdriver/remote/http/common.rb:59:in `new'
        from /Users/#/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/selenium-webdriver-2.42.0/lib/selenium/webdriver/remote/http/common.rb:59:in `create_response'
        from /Users/#/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/selenium-webdriver-2.42.0/lib/selenium/webdriver/remote/http/default.rb:66:in `request'
        from /Users/#/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/selenium-webdriver-2.42.0/lib/selenium/webdriver/remote/http/common.rb:40:in `call'
        from /Users/#/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/selenium-webdriver-2.42.0/lib/selenium/webdriver/remote/bridge.rb:634:in `raw_execute'
        from /Users/#/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/selenium-webdriver-2.42.0/lib/selenium/webdriver/remote/bridge.rb:612:in `execute'
        from /Users/#/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/selenium-webdriver-2.42.0/lib/selenium/webdriver/remote/bridge.rb:369:in `clickElement'
        from /Users/#/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/selenium-webdriver-2.42.0/lib/selenium/webdriver/common/element.rb:54:in `click'
        from /Users/#/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/watir-webdriver-0.6.10/lib/watir-webdriver/elements/select.rb:155:in `select_by_string'
        from /Users/#/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/watir-webdriver-0.6.10/lib/watir-webdriver/elements/select.rb:131:in `select_by'
        from /Users/#/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/watir-webdriver-0.6.10/lib/watir-webdriver/elements/select.rb:64:in `select'
        from aegon.rb:15:in `<main>'

这就是选择列表的HTML:

<select name="werkervaring" class="selectBox" style="display: none;">
   <option value="">Geen voorkeur</option>
   <option value="1">0 - 0,5</option>
   <option value="2">0,5 - 1</option>
   <option value="3">1 - 2</option>
   <option value="4">2 - 5</option>
   <option value="5">5 - 10</option>
   <option value="6">&gt; 10</option>
</select>

请注意style="display: none;"是我一直收到错误的原因。

这是我正在使用的代码:

require 'watir'

b = Watir::Browser.new :phantomjs
b.goto('https://werkenbijaegon.nl/vacatures/?utm_source=aegon.nl&utm_medium=link&utm_content=home&utm_campaign=werkenbij')

b.select_list(:name => 'werkervaring').select '0 - 0,5'

b.close

问题是我如何从隐藏的选择列表中选择0 - 0,5选项?我应该使用其他东西而不是Watir吗?

3 个答案:

答案 0 :(得分:2)

在选择列表之后有一个链接,该链接是select_list的向下箭头。点击箭头后,有uls和lis。有一个你正在寻找的价值。在那里有一个链接。点击它。

b.select_list(:name, 'werkervaring').parent.link.click
b.li(text: '0 - 0,5').link.click

注意,为了确保我们使用正确的列表,我仍然使用werkervaring作为唯一定位器,然后在同一父节点下找到兄弟链接。

答案 1 :(得分:0)

Selenium只允许用户可以实现的交互,因此不允许与隐藏列表进行交互。

如果列表出现在某些操作上(例如鼠标悬停),则需要先执行该操作。如果列表是通过另一个对象(例如select2)公开的,那么您必须通过该元素进行交互。

但是,如果你真的想在不遵循这一点的情况下实现这一点,那么你可以通过selenium执行JavaScript来启用或设置它。

如果以这种方式进行测试的优点是可以辩论的

答案 2 :(得分:0)

b.select_lists[2].select "0 - 0,5"