Selenium Webdriver doesn't open Firefox - ruby

When I start a script with Selenium-Webdriver, the Firefox window that I expect to open never opens. This code used to work, but no longer does:
require 'selenium-webdriver'
p "DEFINING `driver`"
driver = Selenium::WebDriver.for :firefox
p "OPENING GOOGLE"
driver.navigate.to "http://google.com"
element = driver.find_element(:name, 'q')
element.send_keys "Selenium Tutorials"
element.submit
driver.quit
When I run it, "DEFINING `driver`" prints to the console, but the script just hangs there.
The only major thing I did in the past 48 hours was run a virus scan.

You should update your Selenium-Webdriver gem to version 2.45.0

Related

Trouble with Selenium and ChromeDriver

I'm trying to get Selenium working, but I can't seem to figure it out. I've installed the proper Ruby gem, and am trying to execute this code.
require "selenium-webdriver"
driver = Selenium::WebDriver.for :chrome
driver.navigate.to "http://google.com"
element = driver.find_element(:name, 'q')
element.send_keys "Hello WebDriver!"
element.submit
puts driver.title
driver.quit
I'm fairly certain my issue is that I haven't connected the ChromeDriver correctly. How do I link Chrome driver in order to get this to work? It says I need to place it on my PATH. Can someone explain this?
Here is what I downloaded.
Also, here is the error I'm getting:
in `executable_path': Unable to find the chromedriver executable.
There is a Selenium::WebDriver::Chrome.driver_path= method that allows specifying of the chromedriver executable:
# Specify the driver path
Selenium::WebDriver::Chrome.driver_path = "path/to/chromedriver.exe"
# now instantiate chrome driver
driver = Selenium::WebDriver.for :chrome
driver.navigate.to "http://google.com"
element = driver.find_element(:name, 'q')
element.send_keys "Hello WebDriver!"
element.submit
puts driver.title
driver.quit
Place chrome driver depends on your operating system, check this link:
OS Expected Location of Chrome
Linux /usr/bin/google-chrome1
Mac /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome
Windows XP %HOMEPATH%\Local Settings\Application Data\Google\Chrome\Application\chrome.exe
Windows Vista C:\Users\%USERNAME%\AppData\Local\Google\Chrome\Application\chrome.exe
Unable to find the chromedriver executable :
We get this when system is not able to locate chromedriver. We can help system find it by setting environment variable "webdriver.chrome.driver"
If you are running unix/linux use
export webdriver.chrome.driver=<path_where_binary_is_present>
If running on windows :
Go to System Advanced Environment Variables. Add an entry with name webdriver.chrome.driver and value set to path of binary
We can also set this location from program also. Like in java
System.setProperty("webdriver.chrome.driver", ""
Do any one of this and this error should go away

Ruby webdriver doesn't navigate in Chrome

I have just finished setting up the latest versions of Ruby (Ruby 2.0.0-p451 (x64)) and Watir. I have downloaded both the selenium web driver gem and the watir webdriver gem, both installed successfully. I then got the Ruby dev kit, installed that and it installed fine. Lastly, I got the latest version (2.9) of chromedriver.exe and placed it in the bin directory for ruby.
I can start Chrome from a Watir script but I can't navigate to any URL. When Chrome opens a new instance it has one tab and in the URL all it says is "data:,", the page is completely blank. I have used examples and this one works with Firefox but not Chrome
require 'watir-webdriver'
b = Watir::Browser.new :chrome
b.goto 'bit.ly/watir-webdriver-demo'
b.text_field(:id => 'entry_0').set 'your name'
b.select_list(:id => 'entry_1').select 'Ruby'
b.select_list(:id => 'entry_1').selected? 'Ruby'
b.button(:name => 'submit').click
b.text.include? 'Thank you'
(that example came from http://watirwebdriver.com/)
If I remove ":chrome" from the end of the second line, Firefox opens and navigates to the URL as I'd expect, but can't get that to work in Chrome.
If I use the selenium-webdriver with Chrome I get the exact same result: Chrome opens, it has "data:," in the URL and doesn't navigate to the URL in the script.
I also tried this in the Ruby 1.9.3-p545 version of Ruby and I get the exact same result, so I uninstalled that and am now using the 2.0.0 version of Ruby.
How can I get my script to open chrome and navigate to a URL?

Watir Crawler is Stuck after Goto

First time messing with Watir.
browser = Watir::Browser.new
browser.goto 'google.com'
My firefox window pops up and stays stuck on a blank page. When I end the script in the command line, I get:
...ruby-1.9.3-p194/gems/selenium-webdriver-2.29.0/lib/selenium/webdriver/common/socket_poller.rb:95:insleep': Interrupt`
I've do have the selenium-webdriver gem (2.29) installed. My firefox version is 19.
I do not think selenium-webdriver supports Firefox 19. Try with Firefox 18.

How do I run Firefox browser headless with my Ruby script?

I need to be able to run my Ruby scripts with my Hudson builds but don't want a browser window attempting to open.
I tried:
batman:ETW cmiller$ ./createAccount.rb -b
but this did not work.
How can I run my Ruby scripts using FireWatir headless?
Running on Mac OSX
I'd suggest the usage of PhantomJS (http://phantomjs.org/download.html)
Add binaries to your path and run:
MEDBEDbs-iMac:~ medbedb$ irb
1.9.3p392 :001 > require 'watir-webdriver'
=> true
1.9.3p392 :002 > b = Watir::Browser.new :phantomjs
=> #<Watir::Browser:0x..fe670a70a449fe1ca url="about:blank" title="">
1.9.3p392 :003 > b.goto "www.google.com"
=> "http://www.google.md/"
1.9.3p392 :004 > b.url
=> "http://www.google.md/"
1.9.3p392 :005 > b.title
=> "Google"
1.9.3p392 :007 > b.text
=> "+Вы\nПоиск\nКартинки\nКарты\nPlay\nПочта\nДиск\nКалендарь\nЕщё\nВойти\n×\nОткрывай любимые сайты быстрее\nЗагрузить Google Chrome\n\nМолдoва\n\n \nРасширенный поиск\nЯзыковые инструменты\n\nGoogle.md предлагается на: Moldovenească\n\nРекламные программыВсё о GoogleGoogle.com\n© 2013 - Правила и принципы"
I would look at using Watir-Webdriver instead of just plain Watir or Fire-watir. Especially since the only way to work with newer versions of firefox is going to be via Watir-Webdriver.
There's an earlier SO question where the answer covers just this sort of thing, so I'd suggest trying what is described there there first. Can I use Watir to scrape data from a website on a linux server without monitor?
Also since I now know you are using Mac OS, the advice in this thread from the webdriver google group might be more applicable to you
Look at http://watirmelon.com/tag/headless/.
I have followed http://watirwebdriver.com/headless/ on Linux that works for me. In above link the tutorial is intended for Mac
Here is extract of the links, how to use headless browser:
require 'watir-webdriver'
require 'headless'
Now start headless and browser
headless = Headless.new
headless.start
b = Watir::Browser.start 'www.google.com' puts b.title
b.close headless.destroy
Your browser should run headless.
The browser is opening the window as part of its normal operation and behavior.
You could look into using Mechanize, which supplies the ability to navigate a site. It isn't a browser, but uses Nokogiri underneath to let you grab DOM information and manipulate forms, submit them, and retrieve any resulting page changes. Mechanize doesn't support parsing/executing Javascript though, which puts you back into WATIR's realm.

Selenium doesn't work with Cucumber/Capybara (out of the box) - MacOSX

I'm having trouble trying to get a cucumber example to run under selenium. I'm running
Rails3
Cucumber 0.10.0
Capybara 0.4.1.2
Reading the doco on https://github.com/jnicklas/capybara, it would appear that all that I need to get an example to run under selenium is to do the following:
Step 1: Prefix the scenario with #javascript
#javascript
Scenario: User does x
...
Step 2: Configure env.rb to tell capybara which driver to use:
Capybara.javascript_driver = :selenium
When I run:
bundle exec cucumber feature/myfeature.feature
I get the following:
Using the default profile...
F------------F
Failing Scenarios:
cucumber features/myfeature.feature:7 # Scenario: User does x
1 scenario (1 failed)
12 steps (12 skipped)
0m0.012s
No firefox window. Nothing. It runs, hangs and dies.
So to check whether capybara and the selenium webdriver is working, I wrote the following code:
require 'capybara'
require 'capybara/dsl'
Capybara.default_driver = :selenium
class Test
include Capybara
def dotest
visit('http://www.stackoverflow.com')
end
end
Test.new.dotest
And ran it using:
bundle exec ruby /tmp/test.rb
That works. Firefox opens the window and navigates to www.stackoverflow.com.
So how can I get diagnostic information to understand what cucumber is doing to capybara?
I'm running OSX10., Ruby 1.8.7 and Firefox 3.6.13.
Ok... I found out my problem. Somewhere in the bowls of cucumber is a dependency on DatabaseCleaner which is being triggered once when you use the selenium driver. The failure statement:
F______________F
Told me that the failure was occurring in the setup and teardown. Cucumber wasn't reporting the exception and even with the -b it didn't do anything.
How I found the problem:
Add ruby-debug as a gem dependency
Add require "ruby-debug" to env.rb
I added the following statement to env.rb:
Before do
breakpoint; 0
end
Ran the feature using bundle exec. The debugger kicked in.
Type in cat StandardError which will tell the debugger to breakpoint when "StandardError" is thrown. StandardError is the base class for all errors in Cucumber. What we want to do is find out where the error is being thrown and why.
Type in cont to tell the debugger to resume
After adding database-cleaner as a gem dependency, everything went away. Firefox started firing up and things start to work as advertised.
Did you try
bundle exec cucumber feature/myfeature.feature
!!

Resources