Watir-webdriver throws 'not clickable' error even when element is visible, present - ruby

I am trying to automate tests in Ruby using the latest Watir-Webdriver 0.9.1, Selenium-Webdriver 2.53.0 and Chrome extension 2.21. However the website that I am testing has static headers at the top or sometimes static footers at the bottom. Hence since Watir auto-scrolls an element into view before clicking, the elements get hidden under the static header or the static footer. I do not want to set desired_capabitlites (ElementScrollBehavior) to 1 or 0 as the websites I am testing can have both - static header or static footer or both.
Hence the question are:
1) Why does Watir throw an exception Element not clickable even when the element is visible and present? See ruby code ( I have picked a random company website for an example) and the results below.
2) How can I resolve this without resorting to ElementScrollBehaviour?
Ruby code:
require 'watir-webdriver'
browser = Watir::Browser.new :chrome
begin
# Step 1
browser.goto "shop.coles.com.au/online/mobile/national"
# Step 2 - click on 'Full Website' link at the bottom
link = browser.link(text: "Full website")
#check if link exists, present and visible?
puts link.exists?
puts link.present?
puts link.visible?
#click on link
link.click
rescue => e
puts e.inspect
ensure
sleep 5
end
puts browser.url
browser.close
Result:
$ ruby link_not_clickable.rb
true
true
true
Selenium::WebDriver::Error::UnknownError: unknown error: Element is not clickable at point (460, 1295). Other element would receive the click: div class="shoppingFooter"...div
(Session info: chrome=50.0.2661.75)
(Driver info: chromedriver=2.21.371459 (36d3d07f660ff2bc1bf28a75d1cdabed0983e7c4),platform=Mac OS X 10.10.5 x86_64)>
http://shop.coles.com.au/online/mobile/national
thanks!

You can do a click at any element without getting it visible. Check this out:
link.fire_event('click')
BUT It is very very very not good decision as far as it will click the element even if it is not actually visible or in case when it is just impossible to click it (because of broken sticky footer for example).
That's why much better to wait the fooler, scroll the page and then click like:
browser.div(id: "footerMessageArea").wait_until_present
browser.execute_script("window.scrollTo(0, document.body.scrollHeight);")
link.click

The sticky footer is blocking webdriver from performing the click, hence the message that says 'other element would receive the click'.
There are several different ways you can get around this.
Scroll down to the bottom of the page before the click
Hide/Delete the sticky footer before any/all link clicks
Focus on an element below the element you want to click before you perform the click

I Guess your element is visible in the screen.
Before clicking on the element first you have to scroll the webpage so that element is visible then perform the click. Hope it should work.

I had similar issue,
I just used following javascript code with watir:
link = browser.link(text: "Full website")
#browser.execute_script("arguments[0].focus(); arguments[0].click();", link)

Sometimes I have to use .click! which i believe is the fire_event equivalent. Basically something is layered weird, and you just have to go around the front end mess.

Related

Capybara does not find element properly on Edge

I have encountered an issue that really makes me confused. I'm able to find elements on a page but after I interact with elements from bottom of page, I cannot click element that are on top of page without scrolling to top of page. This only happens on Edge browser (Chrome, Firefox work well).
My code is as below:
page.fill_in_form # This part is middle/bottom of page
page.click_button 'SAVE' # This button is middle/bottom of page
page.select_sub_menu 'Category' #This sub menu is top navigation of page
And this is error message.
Unable to find css "#subnav"
My select_sub_menu funciton:
def select_sub_menu(sub_menu)
within('#subnav') { click_link(sub_menu) }
end
It looks like Capybara does not recognise the elements are on top of page after scrolling into middle of page although they are available.
My driver is configured as below:
Selenium::WebDriver::Edge.driver_path = "#{file_path}/MicrosoftWebDriver.exe"
Is there any solution for this?

Capybara Element is not clickable at point

I was trying to use capybara to help me upload vocabulary to memrise.com, but I encounter some problems in its login page.
Here is what I've written.
def sign_in
self.visit 'https://www.memrise.com/login/'
find(".inpt-large[name='username']").set 'my-username' # Step 1
find(".inpt-large[name='password']").set 'my-password' # Step 2
find('input.btn-success.btn-large').click # Step 3
end
It can finish the step1 and setp2 but fail at step3 sometimes.
And below is the error message.
gems/selenium-webdriver-2.53.4/lib/selenium/webdriver/remote/response.rb:70:in `assert_ok': Element is not clickable at point (592.5, 23). Other element would receive the click: <span class="nav-item-btn-text"></span> (Selenium::WebDriver::Error::UnknownError)
Since the only items on the page I can find matching <span class="nav-item-btn-text"></span> are in the fixed header, I'm guessing you're running your tests with too small of a window size, so the actual Login button you want to hit is off the page when the test is run. This means when the test goes to click the button, it needs to scroll the item into view and it does that by scrolling it to the top of the page. That leaves the button behind the fixed header and unable to be clicked. To fix that you can either
increase your window size so the form doesn't need to be scrolled
set the elementScrollBehavior capability to 1 in your driver registration which will cause elements to be scrolled until they're visible at the bottom of the page rather than top.
scroll the page yourself before clicking the button
Additionally is there are reason you're using find(...).set vs just using fill_in for this form?
def sign_in
visit 'https://www.memrise.com/login/'
within('form#login') do
fill_in('username', with: 'my-username')
fill_in('password', with: 'my-password')
click_button('Login')
end
assert_text('You are now logged in') # whatever messagge is shown once login complete
end

links opening in new window - Cheezy page-object ruby gem with selenium

I'm new to page-object and selenium, but so far it's all been going ok. The last elements I'm testing for this page are social media links.
To check that the links are correct i usually click them and then use selenium's driver.current_url to check that the url of the window is what we'd expect for that link, but these social media buttons are opening links in a new window (this is just to their static facebook page rather than a login etc) which causes the test to fail
is there a way of either:
1-forcing the link to open in the current window, or
2-extracting the target url from the link_element?
this is what the tests look like atm:
def test_facebook_button
assert #testpage.facebook_icon?, 'Facebook button not found'
#testpage.facebook_icon_link
sleep 5
assert #browser.current_url == '<facebookurlforsite>', 'Facebook link does not open expected URL'
end
if not i'm guessing i'd just use the actual selenium methods as some sort of exception to manually grab the properties of the link, i'd rather not some elements be in the page model and some out in the test itself but i can see how it's probably necessary
If you are just checking that the link navigates to the right URL, checking the link's href attribute will be the quickest. The PageObject::Elements::Link elements have an href method for this.
Assuming that facebook_icon_link is your link defined by an accessor method, you would call:
facebook_icon_link_element.href
In other words, the assertion could be written as:
assert facebook_icon_link_element.href == '<facebookurlforsite>'

Ruby cucumber watir-webdriver is not identifying the object which is placed in a frame

Watir code which is working but add condition link is not identifying again here :(
require 'spec'
require 'watir'
browser = Watir::Browser.new
pages = { "RCM Workspace Homepage" => "http://rcm-bpmt.apmoller.net/workspace/faces/jsf/workspace/workspace.xhtml" }
Given(/^that I am on the (.*?)$/) do |page|
# Opening new browser and going to the page which is specified
browser.goto(pages[page])
#Maximizing the opened browser window
browser.maximize
end
When(/^I search for (.*?)$/) do |text|
# Ensuring that we have opened expected page only by verifying the page content
browser.html.include?(text).should == true
end
Then(/^I click on Show Filters link$/) do
#Opening the Conditions window by clicking on the show filters link
browser.link(:id, "portletComponentWorkList_viewNormalModeWorkList_viewPanel_showFiltersLink").click
#Clicking on Add condition link which is placed in a frame and it opnes when I click on Show filters link
browser.element(:id, 'portletComponentWorkList_viewNormalModeWorkList_viewPanel_conditionButton').click
end
HTML details:
<A id=portletComponentWorkList_viewNormalModeWorkList_viewPanel_conditionButton onclick="oc.ajax.jsf.doCallback('portletComponentWorkList','portletComponentWorkList:viewNormalModeWorkList:viewPanel:conditionButton');return false;" href="http://rcm-bpmt.apmoller.net/workspace/faces/jsf/workspace/workspace.xhtml#">
Add condition
</A>
In the following line, Watir will look for the element anywhere except for in the frames.
browser.element(:id, 'portletComponentWorkList_viewNormalModeWorkList_viewPanel_conditionButton').click
Unlike other elements, you must tell Watir when an element is in a frame. This is done similar to how you would scope the search of an element to a specific element.
For example, if there is only 1 frame or your element is in the first frame, you can do (noting the addition of the .frame):
browser.frame.element(:id, 'portletComponentWorkList_viewNormalModeWorkList_viewPanel_conditionButton').click
If there are multiple frames, you will need to add parameters to be more specific about which frame to use. For example, if the frame has an id:
browser.frame(:id => 'myframe').element(:id, 'portletComponentWorkList_viewNormalModeWorkList_viewPanel_conditionButton').click

Element is not clickable at point (617, 690.5). Other element would receive the click: <th>...</th> (Selenium::WebDriver::Error::UnknownError)

I get the error above when trying to press the "add" button on the integration test I am trying to implement. I thought that was a delay with the javascript, and I tried the following, but none of them worked:
Then /^I should click "([^"]*)"$/ do |selector|
# if page.should have_selector(selector)
# find(selector).click
# end
# page.execute_script("$(#{selector}).click()")
find(selector).click
end
I tried all of the above, but that didn't solve the problem.
This is hardly a great answer, but I'll give it anyway...
I've only ever seen this problem when testing with Chrome. There are two solutions that worked for me:
Use Firefox instead of Chrome
Instead of find(selector).click, use page.execute_script("$(#{selector}).click()")
But since you've tried the second option and it didn't work, try using Firefox instead. Sorry :(
I found resizing the browser window made a similar problem go away:
Given(/^I open a new browser window$/) do
Capybara.current_session.current_window.resize_to(1000, 1000)
end
I was getting the same error in Capybara when trying to click_button a 'Save' button at the end of a page. Scrolling and bringing the element to view using JS proved to be an effective workaround.
def scroll_to(element)
script = <<-JS
arguments[0].scrollIntoView(true);
JS
Capybara.current_session.driver.browser.execute_script(script, element.native)
element # return the element
end
scroll_to(
find(:xpath, '//button[text()=\'Save\']')
).click
Update
For me, it turned out to be a mismatch between the version of installed Chrome and Chromedriver and no code change was necessary after all.
Chrome 61+ includes modifications to scrolling behaviors. A quick examination of Chromedriver page revealed that I had to upgrade my Chromedriver (from 2.29 to 2.32) in order to make the driver compatible with those changes. This was part of the Chromedriver 2.30 (Supports Chrome v59-61) release notes:
Fixes a bug where Chromedriver fails to click due to page scrolling changes in Chrome 61+.
I had a sticky header that was preventing Selenium from sending the click to the right radio button on both Chrome and Firefox (though Poltergeist worked fine). This is a hack to delete all fixed elements (like sticky headers), and it worked for me:
def kill_sticky_headers
# https://alisdair.mcdiarmid.org/kill-sticky-headers/
script = <<-EOS
(function () {
var i, elements = document.querySelectorAll('body *');
for (i = 0; i < elements.length; i++) {
if (getComputedStyle(elements[i]).position === 'fixed') {
elements[i].parentNode.removeChild(elements[i]);
}
}
})();
EOS
page.execute_script script
end
I was facing a similar problem where I have to check two check boxes one after the other. But I was getting the same above error, hence I added a wait in between my steps for checking the checkboxes. It's working fine and great.
Here are the steps:-
When I visit /administrator/user_profiles
And I press xpath link "//*[#id='1']"
Then I should see "Please wait for a moment..."
When I wait for 5 seconds
And I press xpath link "//*[#id='2']"
Then I should see "Please wait for a moment..."
When I visit /administrator/user_profiles_updates

Resources