Created
August 16, 2011 14:45
-
-
Save dblock/1149247 to your computer and use it in GitHub Desktop.
Selenium Webdriver dropdown selection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'selenium-webdriver' | |
path = File.expand_path("test.html") | |
File.open(path, "w") { |io| io << DATA.read } | |
browser = Selenium::WebDriver.for :firefox # replace :firefox with the browser you're having trouble with | |
begin | |
browser.get "file://#{path}" | |
s1 = browser.find_element(:name, "s1") | |
s1.click | |
sleep(0.5) | |
s1_1 = s1.find_element(:name, "1") | |
s1_2 = s1.find_element(:name, "2") | |
s1_2.click | |
sleep(0.5) | |
submit = browser.find_element(:name, "submit") | |
submit.click | |
sleep(0.5) | |
# should be test.html?s1=2&submit=Submit+Query | |
puts browser.current_url | |
ensure | |
browser.quit | |
File.delete path | |
end | |
__END__ | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>Page Title</title> | |
</head> | |
<body> | |
<form method="get" id="form"> | |
<select name="s1"> | |
<option name="1" selected="true">1</option> | |
<option name="2">2</option> | |
</select> | |
<br> | |
<input type="submit" name="submit"> | |
</form> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice approach. Thanks for posting!
Similarly, I've outlined a couple of different ways to accomplish selecting from a dropdown using Ruby and Selenium WebDriver (along with an example web app). If you're interested you can find it at http://elemental-selenium.com/tips/5-select-from-a-dropdown.
Cheers,
Dave H