处理元素在点 (x, y) 处不可单击。其他元素会收到点击:With Capybara, ruby, Webdriver

Handling Element is not clickable at point (x, y). Other element would receive the click: With Capybara, ruby, Webdriver

我正在编写一个 UI 测试,它转到文章列表并选择 load_more 按钮,直到它不再可用(因为所有文章都已加载)。问题是此时按钮已被隐藏而不是被删除,并返回此错误

Selenium::WebDriver::Error::UnknownError: unknown error: Element <div 
class="">...</div> is not clickable at point (x, y). Other element 
would receive the click: <div class="" id="" data-module-name="">... 
</div>

(Session info: chrome=65.0.3325.181)
(Driver info: chromedriver=2.36.540469 
ruby-2.5.1/gems/selenium-webdriver 
3.11.0/lib/selenium/webdriver/remote/response.rb:69:in `assert_ok'

我已经尝试了几种循环类型,但是对于这个,如果我们假设我正在使用

while world.page.has_load_more?
  world.page.load_more.click
break if world.page.has_load_more? == false
end

问题是我如何告诉 Selenium 这是可以的而不是错误,所以我可以继续我的测试用例(黄瓜)中的下一步。我意识到循环类型的选择也可能不正确,所以请随时提出更改建议。

首先,正如 Rajagopalan 所指出的,您得到的错误不是您的元素不可见(如被 CSS 隐藏),而是它被另一个元素重叠。您可以调用 save_and_open_screenshot 来查看与按钮重叠的元素(可能是固定页脚等?)。确保您已将 window 大小设置得足够大,这样您就不会无意中重叠元素,并确保您是 运行 最新版本的 Capybara,因为它会尝试处理 selenium 中的重叠元素如果可能,将它们滚动到视图中。

其次,您的问题中的循环同时具有 whilebreak 并没有多大意义,因为它们检查的是同一件事。此外,假设 "load more" 按钮正在通过 AJAX 加载并附加到页面,您可能需要等待新元素加载后再检查按钮是否存在(click 不等待用于完成操作,因为它不知道会触发什么操作 could/would)。那会变成一个类似

的结构
 while page.has_button?('Load More')
   # Get the current number of visible articles
   article_count = page.all('div.article').count # Use whatever selector would match an article in the list
   # click the button
   page.click_button('Load More')
   # ensure more articles have loaded
   expect(page).to have_css('div.article', minimum: article_count + 1)
end

注意:您可能希望将其更改为使用您正在使用的任何页面对象方法(has_load_more?,等等),但一般逻辑应该是正确的