Ruby 使用watir webdriver逐个选择选项-

Ruby 使用watir webdriver逐个选择选项-,ruby,watir,Ruby,Watir,我有三个下拉列表,每个下拉列表取决于前面的值,我必须逐个选择每个选项,然后单击按钮*这样做时,获取以下错误*元素不再附加到DOM(Selenium::WebDriver::Error::StaleElementReferenceError) 也尝试过: b.select_list(:id, "MainContent_drpVehicleType").when_present.options.each do |option| option.select b.select_list

我有三个下拉列表,每个下拉列表取决于前面的值,我必须逐个选择每个选项,然后单击按钮*这样做时,获取以下错误*元素不再附加到DOM(Selenium::WebDriver::Error::StaleElementReferenceError)

也尝试过:

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
试试下面的方法

 b.driver.manage.timeouts.implicit_wait = 3

假设每个选项都是唯一的,您可以尝试:

  • 获取依赖列表中的最后一个选项
  • 在当前列表中进行选择
  • 等待步骤1中的选项不再出现
  • 以下实现了这一想法(尽管由于我没有类似的页面可供测试,因此尚未对其进行测试):

    请注意:

    • 代码假定依赖列表的最后一个选项将始终更改。如果不是这样,您可能需要对照依赖列表的所有选项进行检查

      • 我关心的一个问题是点击后会发生什么?如果页面刷新或执行了某些操作,那么您很可能会被上面的嵌套方法烤到,因为在第一次单击之后,无法更改第三个列表中的选择并再次单击

        如果以上这些都不起作用,那么当您在列表中做出选择时,您可能需要进行一些窥探,以查看发生了什么类型的HTML请求(可能是对RESTAPI的请求)。您可以使用它构建自己的多维数组来映射选择。或者,您可以使用类似于上面的循环结构获取各种选择列表内容,然后循环并单击

        一旦你有了地图,使用类似于上面的嵌套循环逐维迭代它,但在最里面的循环中执行你的所有操作,这相当于这个psuedo代码

        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
        

        问题可能是watir试图在下拉列表刷新之前访问选项。您需要一种方法来确定下拉列表何时完成刷新-可能是检查下拉列表的值是否已更改(请注意,这仅在每组选项都是唯一的情况下才起作用).谢谢回复..但我有100多个选项值,无法设置每个值..是否有任何方法可以确定等待选项刷新…每个“车型”的“品牌”列表是否唯一?同样,每个“make”的“models”列表是否唯一?是的值都是唯一的。您可以在选择值后使用睡眠5秒钟。你试过了吗?虽然这可能有效,但考虑到有100多个选项正在测试,速度可能会很慢。谢谢你的回复。仍然会发生相同的错误哇。。当您尝试单击该按钮时,该按钮不再附加到dom。我可以理解选择列表等,但按钮?真正地即使前面有这样的睡眠?哇。谢谢你的回复…得到相同的错误…让我解释清楚我的问题1)在Vechile类型中有三个选项AUTO、HD、MARINE 2)如果我选择了Vechile类型,则需要在Make下拉列表中加载与tat相关的Vechile Make 3)在tat选择Model选项后,(它只包含所有Make的一个值)。4) 点击搜索按钮5)后,现在它将显示与搜索相关的所有值6)这样我必须检查每个选项并点击搜索按钮,需要显示与搜索相关的所有值如果您硬编码一个特定示例(即,而不是循环所有选项),是否有任何错误?如果不是的话,那么我猜Chuck是对的,有一些令人耳目一新的东西。访问重现问题的页面或html确实会有帮助。是的,chuck是正确的…单击按钮后,它正在刷新..并且很抱歉无法提供对tat页面的访问..感谢您提供宝贵的时间..页面没有刷新..并且没有验证任何其他内容..只是我需要显示与此相关的信息特定的下拉列表(向量类型、Make、Model)…任何想法一旦刷新,所有这些对象集合几乎都会失效。与使用动态集合相比,您可能需要获取列表的内容,提取到您自己的集合(可能是多维数组),然后使用循环中存储的值来进行选择。我对伪代码进行了一些增强,以使其更清晰(我希望)我认为可行的方法
        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
        
        gather the types and store
        for each stored-type do
          pick the type 
          gather makes available for that type and store
          for each make of that type do
            pick the type, then make
            gather models available for that type
            for each model of that make do
              goto starting point
              select type
              select make
              select model
              click
              validate what should happen
            end
           end
         end