如何在 Capybara 中滚动模态 window

How to scroll a modal window in Capybara

我正在使用 Selenium WebDriver、RSpec 和 Capybara 进行测试。

我让程序成功地填写了模态中的一些字段window。现在我想点击这个模式 window 底部的按钮。乍一看看不到这个按钮,所以Capybara需要在模态里向下滚动window.

网页的两个相关代码片段:

<div class = “modal”> </div>
   <button class=”btn …..”> TextOnButton ::after </button>

我试过了:

within('.modal') do
      find('.btn', text: ‘TextOnButton').scrollIntoView(true)
end

但收到错误信息。

Unable to find visible css ".btn" with text "TextOnButton"

我试过了:

within('.modal’) do
       page.execute_script 'window.scrollBy(0,100)'
end

但随后他滚动了主要 window 而不是模态 window。

假设您的 HTML 片段不正确并且按钮元素实际上包含在模态中(如您的文字描述),那么您可以尝试类似

within('.modal') do
  btn = find(:button, 'TextOnButton', visible: :all)
  execute_script('arguments[0].scrollIntoView(true)', btn)
  btn.click
end

我知道您已经在模态上执行代码,但只是想知道您是否先使用 driver.switchTo().window(handle) 切换到该模态?

这项工作:

execute_script('arguments[0].scrollIntoView(true)', element)