b.select_list(:id, "MainContent_drpVehicleType").when_present.options.each do |option|
option.select
b.select_list(:id, "MainContent_drpMake").when_present.options.each do |option|
option.select
b.select_list(:id, "MainContent_drpModel").when_present.options.each do |option|
option.select
b.button(:id,"MainContent_imgbtnsearch").click
end
end
end
我有三个下拉菜单 每个下拉菜单都取决于以前的值 我必须一个一个地选择每个选项然后单击 Button 。 *当这样做时出现以下错误* 元素不再附加到 DOM(Selenium::WebDriver::Error::StaleElementReferenceError)
也尝试过:
b.driver.manage.timeouts.implicit_wait = 3
最佳答案
假设每个选项都是唯一的,您可以尝试:
- 获取依赖列表中的最后一个选项
- 在当前列表中进行选择
- 等待第 1 步中的选项不再出现
下面实现了这个想法(虽然它还没有被测试,因为我没有类似的页面来测试):
b.select_list(:id, "MainContent_drpVehicleType").when_present.options.each do |option|
#Select a vehicle type and wait for the last make option to no longer appear
last_make_option = b.select_list(:id, "MainContent_drpMake").when_present.options.last
option.select
last_make_option.wait_while_present
b.select_list(:id, "MainContent_drpMake").when_present.options.each do |option|
#Select a make and wait for the last model option to no longer appear
last_model_option = b.select_list(:id, "MainContent_drpModel").when_present.options.last
option.select
last_model_option.wait_while_present
b.select_list(:id, "MainContent_drpModel").when_present.options.each do |option|
option.select
b.button(:id,"MainContent_imgbtnsearch").click
end
end
end
注意:
- 代码假定从属列表的最后一个选项将始终更改。如果不是这样,您可能需要检查依赖列表的所有选项。
关于ruby - 使用 watir-webdriver- 一个一个地选择选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15742278/