Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Element is not clickable error Ruby / Watir

Tags:

ruby

watir

In my test, I am attempting to hit etsy.com, do a search, click on a result, and add the item to my cart. I'm able to do everything up until the point where I attempt to click on the 'add to cart' button. The code below actually works in the IRB so I know my locator is solid, but when I run the test I get an element is unclickable at point error

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)

Here's my test

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

And here is the page HTML for the button.

<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>
like image 592
Jen Avatar asked Oct 05 '17 14:10

Jen


People also ask

How do you select an element in Watir?

Elements are located by creating a Selector Hash, which Watir translates into the potentially complicated information the driver needs to know to identify the element. The special cases will be highlighted below, but Watir Locators: Accept String values for exact matching. Accept RegExp values for partial matching.

Does Watir use selenium?

Watir is an open-source web application testing framework that is designed to make writing Selenium tests simple and efficient. Built on Selenium's Ruby language bindings, Watir is able to drive the browser in the same way humans do.

What is GEM Watir?

Watir stands for Web Application Testing In Ruby It facilitates the writing of automated tests by mimicing the behavior of a user interacting with a website.


1 Answers

Depending on the browser width (and likely other factors), there may be dialogs floating over the add to cart button. For example, when the test failed for me, there was a get started dialog on top of the button. Chrome attempts to click by a location. If another element is on top of your element at that location, Chrome will throw the exception.

enter image description here

The easiest solution is to bypass Chrome's check by directly triggering the click event:

# 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)

Other solutions that may conditionally work:

  • Maximize the browser before clicking the button - browser.window.maximize. This can move the floating element away from the button.
  • Close the floating dialog.
like image 102
Justin Ko Avatar answered Nov 10 '22 22:11

Justin Ko