I'm trying to use capybara
for web scraping. Namely, making it click the link, which is a span
actually, but has onclick
handler attached:
#!/usr/bin/env ruby
require 'rubygems'
require 'bundler/setup'
require 'capybara/dsl'
include Capybara::DSL
Capybara.default_driver = :poltergeist
visit 'http://example.com'
p current_url
sleep 10
first('#some-id').click
sleep 10
p current_url
But judging from current_url
it fails. For now I'm using poltergeist
driver. When I run this code against the other (test) page with span having onclick
handler attached, it works. How can I figure out what's the issue?
What I did. Installed selenium-webdriver
. Switched to driver :selenium
:
Capybara.default_driver = :selenium
And added byebug
after clicking the link. And now I see that clicking the link redirects to the other site, but also opens new window. However, capybara
reports url didn't change.
UPD The issue was that clicking the link opened two new windows (in terms of capybara
, from user point of view only one more window appeared). That's why capybara
reported the same url. And with Thomas Walpole's help, I was able to get to the new url of initial window:
within_window(Capybara.current_session.windows[-2]) { p current_url }
See this comment for alternative ways of choosing the window.