1

我希望能够单击(查看可用选项/帐户列表,开始时似乎是隐藏的),然后dijit 小部件“select_list”设置为某个值。

我可以使用找到对象

@@ie.element(:css, "#accountSwitcherSelect.dijitDownArrowButton").flash
#works

但不能使用 click 或 set :

@@ie.element(:css, "#accountSwitcherSelect.dijitDownArrowButton").click
#no errors, but doesn't do anything

@@ie.element(:css, "#accountSwitcherSelect.dijitSelectLabel").set("Account2")    
#NoMethodError: undefined method `set' for <Watir::HTMLElement:0x4d6af60>

@@ie.element(:css, "#accountSwitcherSelect.dijitSelectLabel").send_keys("Account3")
#NoMethodError: undefined method `send_keys' for <Watir::HTMLElement:0x4d6af60>

过去,该站点是使用纯 html 实现的,并且有一个 select_list,所以

@@ie.select_list(:name, "link").set(/Account1/i)

工作得很好。

这是网站背后的当前 html:

<table id="accountSwitcherSelect" class="dijit dijitReset dijitInline dijitLeft dijitDownArrowButton dijitSelectFixedWidth dijitValidationTextBoxFixedWidth dijitSelect dijitValidationTextBox" lang="en-US" cellspacing="0" cellpadding="0" aria-haspopup="true" role="listbox" data-dojo-attach-point="_buttonNode,tableNode,focusNode" style="-moz-user-select: none; width: 207px;" tabindex="0" widgetid="accountSwitcherSelect" aria-expanded="false" aria-invalid="false">
  <tbody role="presentation">
    <tr role="presentation">
      <td class="dijitReset dijitStretch dijitButtonContents" role="presentation">
        <div class="dijitReset dijitInputField dijitButtonText" role="presentation" data-dojo-attach-point="containerNode,_popupStateNode" popupactive="true">
          <span class="dijitReset dijitInline dijitSelectLabel dijitValidationTextBoxLabel " role="option">Account1</span>
        </div>
        <div class="dijitReset dijitValidationContainer">
      </td>
      <td class="dijitReset dijitRight dijitButtonNode dijitArrowButton dijitDownArrowButton dijitArrowButtonContainer" role="presentation" data-dojo-attach-point="titleNode">
        <input class="dijitReset dijitInputField dijitArrowButtonInner" type="text" role="presentation" readonly="readonly" tabindex="-1" value="? ">
      </td>
4

1 回答 1

0

假设“选择列表”与digit.form.Select 文档页面上的类似,您需要:

  1. 单击下拉箭头(输入元素)
  2. 单击包含所需文本的单元格(td 元素)

在您的情况下,它看起来像:

@@ie.input(:class => 'dijitArrowButtonInner').click
@@ie.td(:class => 'dijitMenuItemLabel', :text => /Account1/i).click

这是一个工作示例(尽管它依赖于仍然在线可用的 dijit css 文件)。

require 'watir-webdriver'

browser = Watir::Browser.new
browser.goto "data:text/html,#{DATA.read}"
browser.input(:class => 'dijitArrowButtonInner').click
browser.td(:class => 'dijitMenuItemLabel', :text => 'Arizona').click
sleep(10)  # Just so that you can see the dropdown has changed
browser.close

__END__

<html>
    <head>
        <link rel="stylesheet" href="https://dojotoolkit.org/reference-guide/1.9/_static/js/dijit/themes/claro/claro.css">
        <script>dojoConfig = {async: true, parseOnLoad: true}</script>
        <script src="https://dojotoolkit.org/reference-guide/1.9/_static/js/dojo/dojo.js"></script>
        <script>require(["dojo/parser", "dijit/form/Select"]);</script>
    </head>
    <body class="claro">
        <div name="select3" value="AL" data-dojo-type="dijit/form/Select">
            <span value="AL"><b>Alabama</b></span>
            <span value="AZ"><i>Arizona</i></span>
        </div>
    </body>
</html>

Watir-经典

上述解决方案适用于 watir-webdriver,但不适用于 watir-classic。单击下拉列表(实际上是一个表格)不会触发足够的事件来打开下拉列表。作为一种解决方法,您似乎可以使用它send_keys来触发正确的事件。

假设browser打开到具有上述 html 的页面,以下将起作用:

browser.table(:id => 'dijit_form_Select_0').send_keys :enter
browser.td(:class => 'dijitMenuItemLabel', :text => 'Arizona').click

请注意,此代码中的表格是选中的选项(关闭下拉菜单时)。

于 2013-11-14T14:38:34.040 回答