【问题标题】:Element is not clickable error Ruby / Watir元素不可点击错误Ruby / Watir
【发布时间】:2019-05-20 00:58:38
【问题描述】:

在我的测试中,我尝试访问 etsy.com,进行搜索,点击结果,然后将商品添加到我的购物车。在尝试单击“添加到购物车”按钮之前,我可以做所有事情。下面的代码实际上在 IRB 中工作,所以我知道我的定位器是可靠的,但是当我运行测试时,我得到一个元素在点错误时是不可点击的

C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/selenium-webdriver-3.6.0/lib/selenium/webdriver/remote/response.rb:71:in 'assert_ok': unknown error: Element is not clickable at point (930, 586) (Selenium::WebDriver::Error::UnknownError) (Session info: chrome=61.0.3163.100)

这是我的测试

require 'watir'

# test that a user can search for and add an item to shopping cart
b = Watir::Browser.new :chrome

begin
  b.goto "http://etsy.com"
  b.text_field(:id => 'search-query').set 'bacon is my spirit animal coaster'
  b.button(:value => 'Search').present?
  b.button(:value => 'Search').click
  b.p(:text => /Bacon Spirit Animal Coaster/).click
  b.select_list(:id => 'inventory-variation-select-0').option(:text => 'Single ($8.00)').select
  b.button(:text => /Add to cart/).click

  if b.text.include?("item in your cart")
    puts "Test passed!"
  else
    puts "Test failed!"
  end 

ensure
  b.close
end

这是按钮的页面 HTML。

<button class="btn-transaction" type="submit">
            <div class="btn-text">Add to cart</div>
            <div class="ui-toolkit">
                <div class="btn-spinner spinner spinner-small display-none"></div>
            </div>
        </button>

【问题讨论】:

    标签: ruby watir


    【解决方案1】:

    根据浏览器宽度(以及可能的其他因素),添加到购物车按钮上可能会出现对话框。例如,当我的测试失败时,按钮顶部有一个开始对话框。 Chrome 会尝试按某个位置进行点击。如果另一个元素位于该位置的元素之上,Chrome 将抛出异常。

    最简单的解决方法是绕过Chrome的检查,直接触发点击事件:

    # Watir > 6.8.0:
    b.button(:text => /Add to cart/).click! # note the exclamation mark
    
    # Watir < 6.8.0:
    b.button(:text => /Add to cart/).fire_event(:onclick)
    

    其他可能有条件的解决方案:

    • 在单击按钮之前最大化浏览器 - browser.window.maximize。这可以将浮动元素从按钮上移开。
    • 关闭浮动对话框。

    【讨论】:

    • 您不仅花时间帮我解答,而且还解释了原因——真的很棒。非常感谢 - 谢谢!
    猜你喜欢
    • 1970-01-01
    • 2016-12-29
    • 1970-01-01
    • 1970-01-01
    • 2018-03-16
    • 2012-08-08
    • 1970-01-01
    • 2023-01-19
    • 2021-02-26
    相关资源
    最近更新 更多