如何用 break 停止无限循环

How to stop the infinite loop with a break

def find_a_specific_security_channel_inside_a_table
  expect(page).to have_css('#datatableGeneric')

  loop do   
    td = page.all('td', text: 'NAME')
    Timeout.timeout(5) do
      if td.empty? 
        click_link ('Next')
        sleep 1
      else
        find('#datatableGeneric', :text => 'NAME').click
        break
      end
    end
  end
  return true
end

应该遍历table的内容直到找到('NAME')字符串,最后跳出循环。但是并没有跳出循环。

您只是从超时块中断,而不是从主循环中断。

loop do
  td = page.all('td', text: 'NAME')
  break_loop = false

  Timeout.timeout(5) do
    if td.empty? 
      click_link ('Next')
      sleep 1
    else
      find('#datatableGeneric', :text => 'NAME').click
      break_loop = true
    end
  end

  break if break_loop
end