【问题标题】:How to select value in dropdown menu in ruby watir without knowing the value?如何在不知道值的情况下在 ruby​​ watir 的下拉菜单中选择值?
【发布时间】:2018-08-11 21:58:18
【问题描述】:

我正在尝试从下拉菜单中选择某些内容,但不知道有哪些选项。代码需要为许多用户工作,每个人都有不同的价值观。

<select name="aspx_accountinformation_accountstatementsearch_ascxTRANSACTION_HISTORY1dfe5e76-b83a-4957-84c3-16e8ae795e9c$ddlAccount" id="aspx_accountinformation_accountstatementsearch_ascxTRANSACTION_HISTORY1dfe5e76-b83a-4957-84c3-16e8ae795e9c_ddlAccount" class="selectM">
                        <option value="Selectare">Selectare</option>
                        <option value="226523531-Card">226523531-232-MDL</option>

                    </select>

目前我只知道如何在知道选项的情况下进行操作,如下所示:

browser.option(:text,'226523531-232-MDL').click

【问题讨论】:

    标签: ruby testing select watir dropdown


    【解决方案1】:

    我不确定我是否正确理解了你的问题,

    如果你想一直选择第一个选项,那么

    locator='aspx_accountinformation_accountstatementsearch_ascxTRANSACTION_HISTORY1dfe5e76-b83a-4957-84c3-16e8ae795e9c$ddlAccount'
    
    b.select_list(name: locator).options.first.select
    

    如果你想选择一些随机选项,那么

    b.select_list(name: locator).options.to_a.sample.select
    

    【讨论】:

    • 我想每次都选择一个选项,并且我希望能够指定它是第一个还是第二个、第三个等。我试过你的代码,但我收到一个错误 C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/watir-6.10.3/lib/watir/elements/element.rb:664:in rescue element_call':元素定位,但30秒后超时,等待#<:select: located: false>"aspx_accountinformation_accountstatementsearch_ascxTRANSACTION_HISTORY1dfe5e76-b83a-4957-84c3-16e8ae795e9c$ddlAccount", :tag_name=>"select"}> 存在;也许在 iframe 中查看? (Watir::Exception::UnknownObjectException) `
    • @Alexandru Pereira 然后尝试使用 b.select_list().Options[1].select 和第二个选项使用 [2]
    • 我把它改成了这个browser.select_list().option[1].select,但是得到了错误 C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/watir-6.10.3/lib/watir /elements/element.rb:688:in method_missing': undefined method []' for #<:option:0x0000000005db9b10> (NoMethodError)
    • 你需要使用复数形式“optionS”:browser.select_list().options[1].select
    • 阅读我的评论,我写的是options 而不是option
    猜你喜欢
    • 1970-01-01
    • 2015-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多