Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 如何使用页面上的多个元素进行迭代_Ruby_Capybara - Fatal编程技术网

Ruby 如何使用页面上的多个元素进行迭代

Ruby 如何使用页面上的多个元素进行迭代,ruby,capybara,Ruby,Capybara,我有几个按钮可以在同一页上单击。我如何迭代并单击它们中的每一个 def btnConectar() elements = all("button[data-control-name='srp_profile_actions']").count puts elements first("button[data-control-name='srp_profile_actions']").click find("section[class=modal]")

我有几个按钮可以在同一页上单击。我如何迭代并单击它们中的每一个

def btnConectar()


   elements = all("button[data-control-name='srp_profile_actions']").count 

    puts elements

    first("button[data-control-name='srp_profile_actions']").click 
    find("section[class=modal]")
    find("button[class='button-primary-large ml1']").click

end

all
返回类似于Capybara::Result对象的数组。您可以使用标准的ruby可枚举方法对其进行迭代

all("button[data-control-name='srp_profile_actions']").each do |el|
  el.click
  find("section[class=modal]") # Not sure what this is for - if it's an expectation/assertion it should be written as such
  click_button(class: %w(button-primary-large ml1) 
end
只要点击按钮不会导致浏览器移动到另一个页面,这项功能就可以正常工作


如果单击确实导致浏览器移动到另一个页面,那么Capybara::Result对象中的所有其余元素都将变得陈旧(导致在下一次迭代中出现陈旧元素引用错误),您将无法再进行迭代。如果这是你的情况,那么详细说明你到底在做什么将是必要的。单击
按钮后,页面上是否仍然存在原始按钮,或者您是否可以通过反复单击第一个匹配按钮来进行迭代?如果它仍然存在,是否以任何方式更改以表明它已被单击,或者页面上按钮的数量/顺序是否保证稳定?如果您在第一次和第二次迭代中发布了HTML片段,这可能会有助于理解。

单击每个按钮可能会重复什么?它是否会更改当前页面(从而使其余按钮过时)或只是更改当前页面上某些内容的状态?在我的情况下,在单击所需按钮后,会在前面启用一个模式,并在后面的屏幕上禁用我要单击的其他按钮,有必要单击前屏幕上的按钮,然后尝试单击后屏幕上的其他按钮,因此它不会在屏幕上的所有按钮上重复。我希望我的解释是清楚的。@MayconSilva您需要提供关于我提供的代码产生的错误的实际细节-仅仅说它不起作用并不能告诉我们anything@MayconSilva如果单击按钮只会导致页面更改,请使用您需要回答的问题更新答案,我已经尝试了上面的语法,但是还没有用页面上的按钮进行迭代。它单击第一个按钮,但不继续迭代,并显示以下错误:陈旧元素引用:“元素未附加到文档页面”更新/编辑您的问题,而不是添加包含更多问题的答案。
 def btnConectar()

        page.all("button[data-control-name='srp_profile_actions']").each do |el|

        while page.has_css?("button[data-control-name='srp_profile_actions']")  

        el.click #Click the button
        find("section[class=modal]") #Modal mapping
        click_button(class: %w(button-primary-large ml1)) #Click the button
        sleep 3

        end

    end

end