Capybara not finding elements on page - ruby

I have a landing page that contains 10 links which i need to click through and load. I'm using Capybara with Selenium web driver to create a RSpec test that will load the website, login, go to landing page, click the first dashboard link in landing page, return to landing page, click second link, etc.
Whenever Capybara returns to the landing page it always returns ElementErrorNotFound when attempting to click the 2nd link. My guess is that the JavaScript isn't loading before the element is clicked, but isn't Capybara now smart enough to wait for the page to load?

I am not to familiar with Ruby and Capybara but Selenium has Implicit Waits which should take care of that issue. http://docs.seleniumhq.org/docs/04_webdriver_advanced.jsp
You could always loop until it returns the element you are looking for, also.

Related

Problem interacting with elements inside an iframe

I'm able to retrieve data inside an iframe using
browser.iframe
But when I try to interact with elements inside this iframe, like for ex. click on a button, watir won't locate any of them.
I tried with all kind of elements inside this iframe, but nothing happens.
There could be more than one iframe on your page so you might be targeting the wrong one (just writing browser.iframe will target the first iframe on page.
You can could inspect the page and check if there are more than one iframe, or type:
puts "there are #{browser.iframes.count} on page"
and run the script again.
ideally iframe has an id so you could write
my_iframe = browser.iframe(id: "something")
my_iframe.buttons.last.click
# or
my_iframe.button(text: "ok").click
if it's not a secret, right click => inspect page you are automating and screenshot/paste html here. Or share the whole code that doesn't work for you.

How to click an element on the page without loading it with capybara?

Is it possible to click on the element without loading the page using
capybara and Ruby?
No that is not possible. You need to load the page for the browser to know what to do when the element is clicked.
If the element being clicked just triggers a web request and you know what that web request is then you could just make that request using any of the ruby networking libraries but then you may run into issues with referrer, cookies, etc.

How can I use mechanize to click button to webscrape a page to get information?

I'm looking to scrape the contents of a page that requires you to press an arrow button in which, information will appear via jquery rather than loading a new page. Since there needs to be a button click, I'm using mechanize for this part instead of nokogiri. What I have so far is
url = "http://brokercheck.finra.org/Individual/Summary/1327992"
mechanize = Mechanize.new
page = mechanize.get(url)
button = page.at('.ArrowExpandDsclsr.faangledown')
new_page = mechanize.click(button)
new_page.at('#disclosuredetails')
It appears that new_page still doesn't show the page with the newly loaded information. Anyone know why that is?
The button you're trying to get mechanize to click is not a "regular" button, it's a bit more dynamic. It uses javascript / ajax to fetch the relevant data when clicked.
Mechanize doesn't render the DOM of a web page nor it provides a way to have javascript interact with the page. Thus it's not suitable for interacting with dynamic pages depending on javascript for their functionality.
For such cases, I'd suggest phantomjs, either standalone or through capybara / poltergeist if you'd prefer interacting with it via ruby.

Watir-webdriver: how to force the click method to wait for the page to load completely

I recently started using Watir-Webdriver after previously using Watir since I needed some functionality which was present in webdriver but not in Watir. I am facing a problem in watir-webdriver which I never faced in Watir.
Whenever one used the click method on a button in Watir, it used to implicitly wait till the new page is loaded completely. I read on the Watir-webdriver docs that the waiting and in this question that the waiting in watir-webdriver has been changed so as to accommodate the dynamic changes made to a webpage made by AJAX. Now, the page which I am automating does not have any AJAX elements, it only contains static elements.
But when I used the click method in watir-webdriver, it does not wait for the next page to load implicitly and executes the next line in the call immediately. I could use is wait_until_present but to do it for lots of pages seems like an overkill to me when waiting for a page to load on a click seems to be the standard behavior.
Am i missing some options which I need to toggle so that the click method of Watir is emulated in watir-webdriver?
If you are having problems where the browser is indicating the page is loaded, but stuff is still happening client side (might be javascript code executing, or css logic etc) then I'd suggest using the 'when_present' decorator in front of your action.
That's a little cleaner than having to put in a separate wait step, and is a lot better than fixed sleep periods etc.
browser.button(:how => 'what').when_present.click
you may also wait for the div to load if you want the content to load but not click.
browser.div(:how => 'what').wait_until_present

How can I make cucumber with capybara and selenium fire Ajax on page load

I have the following as a cucumber story:
#javascript
Scenario: Showing the page
Given I am a logged in user
And there is a member with a site
And I go to the home page
.....
When that home page is loaded, there is a drop down that is populated via AJAX on page, in the selenium run browser test the
$(document).ready(function(){})
is not run and as such a drop down select that I need to use is not present. Is there any way that I can force Selenium to fire a javascript event for page load via a step maybe
When the page is loaded information about members is retrieved via AJAX
or something like that. I need that dropdown box, but due to some Action Caching in other parts of the application that use the navigation drop downs but should only sometimes have this extra drop down I load them in after with javascript, I can't really change that.
Any Ideas?
EDIT:
It turns out I had set up some test data wrong and that was failing a condition for it, so it seems that as usual the library is fine and I made a mistake, so it seems this will be fine.

Resources