选择第二个下拉值Watir-Webdriver

时间:2013-03-09 12:00:07

标签: ruby watir

设计:

首次下拉:

<select id="MainContent_drpVehicleType" style="width:175px;" name="ctl00$MainContent$drpVehicleType">
<option value="">- SELECT -</option>
<option value="1" title="AUTO">AUTO</option>
<option value="2" title="HD">HD</option>
<option value="3" title="MARINE">MARINE</option>
</select> 

第二次下拉:

<select id="MainContent_drpMake" style="width:175px;" name="ctl00$MainContent$drpVehicleType">
<option value="1" title="ACURA">ACURA</option>
<option value="2" title="ALFA ROMEO">ALFA ROMEO</option>
<option value="74" title="ALLIS CHALMERS LIFT TRUCK">ALLIS CHALMERS LIFT TRUCK</option>
<option value="75" title="ALLIS CHALMERS TRACTOR">ALLIS CHALMERS TRACTOR</option>
<option value="4" title="AMERICAN MOTORS">AMERICAN MOTORS</option
</select>

用于执行的代码:

b.select_list(:id, "MainContent_drpVehicleType").select("AUTO")

b.select_list(:id, "MainContent_drpMake").select("ACURA")

并尝试了

`b.select_list(:id, "MainContent_drpMake").wait_until_present.option(:text, 'ACURA')`

**我的问题是从第一次下拉菜单中选择“自动” 并且无法从第二个下拉列表中选择“ACURA”

执行时出错:

C:/Ruby193/menu.rb:23:in `<main>': 
C:/Ruby193/lib/ruby/gems/1.9.1/gems/watir-webdriver-0.6.2/lib/watir-webdriver/el
ements/select.rb:218:in `no_value_found': "ACURA" not found in select list (Wati
r::Exception::NoValueFoundException)
        from C:/Ruby193/lib/ruby/gems/1.9.1/gems/watir-webdriver-0.6.2/lib/watir
-webdriver/elements/select.rb:152:in `rescue in select_by_string'
        from C:/Ruby193/lib/ruby/gems/1.9.1/gems/watir-webdriver-0.6.2/lib/watir
-webdriver/elements/select.rb:149:in `select_by_string'
        from C:/Ruby193/lib/ruby/gems/1.9.1/gems/watir-webdriver-0.6.2/lib/watir
-webdriver/elements/select.rb:131:in `select_by'
        from C:/Ruby193/lib/ruby/gems/1.9.1/gems/watir-webdriver-0.6.2/lib/watir
-webdriver/elements/select.rb:64:in `select'
        from C:/Ruby193/menu.rb:23:in `<main>'**

1 个答案:

答案 0 :(得分:5)

听起来您选择第一个下拉列值后会填充第二个下拉列表。在这种情况下,您需要等待填充第二个下拉列表(而不是显示第二个下拉列表)。

您可以等待显示特定选项,然后执行以下操作进行设置:

#The option element that you want:
option = b.select_list(:id, "MainContent_drpMake").option(:text => "ACURA")

#Wait for the option to appear
option.wait_until_present

#Set the option
option.select

或者,如果您想在一行上执行此操作,可以使用when_present

b.select_list(:id, "MainContent_drpMake").option(:text => "ACURA").when_present.select