2017-10-05 231 views
1

在我的測試中,我嘗試點擊etsy.com,執行搜索,單擊結果並將項目添加到我的購物車。我能夠做所有事情,直到我嘗試點擊「添加到購物車」按鈕。下面居然代碼工作的IRB,所以我知道我的定位是實心的,但是當我運行測試,我得到一個元素是指向錯誤無法點擊元素不可點擊錯誤Ruby/Watir

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> 

回答

4

根據瀏覽器的寬度(以及其他可能的因素),可能會出現對話框懸浮在添加到購物車按鈕上。例如,當我的測試失敗時,按鈕上方出現一個啓動對話框。 Chrome嘗試按位置點擊。如果另一個元素位於該位置的元素上方,則Chrome會拋出異常。

enter image description here

最簡單的方法是通過直接觸發點擊事件繞過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。這可以將浮動元素從按鈕移開。
  • 關閉浮動對話框。
+2

您不僅花時間幫助我解答問題,還解釋了爲什麼 - 真的很棒。我非常感謝 - 謝謝! – Jen