RubyのSelenium WebDriver(Selenium 2.0)クライアントを使用して選択されたオプションを設定するにはどうすればよいですか
-
10-10-2019 - |
質問
新しいRuby Selenium-Webdriverに慣れようとしています。これは、以前のバージョンのSeleniumやそれに沿ったRubyドライバーよりも直感的であると思われるためです。また、古いセレンを窓のRuby 1.9.1で動作させるのに苦労したので、代替品を探していると思いました。これまでのところ、私は自分のスクリプトでこれをやった:
require "selenium-webdriver"
driver = Selenium::WebDriver.for :firefox
driver.get "https://example.com"
element = driver.find_element(:name, 'username')
element.send_keys "mwolfe"
element = driver.find_element(:name, 'password')
element.send_keys "mypass"
driver.find_element(:id, "sign-in-button").click
driver.find_element(:id,"menu-link-my_profile_professional_info").click
driver.find_element(:id,"add_education_btn").click
country_select = driver.find_element(:name, "address_country")
だから基本的に私は自分のサイトにログインして、ユーザープロファイルに教育エントリを追加しようとしています。オプションを備えた選択ボックスへの参照があります(Country_Select変数)。 ..新しいクライアントでこれを行う方法がわかりません。私がすることを考えることができる唯一のことは、私が望むものを見つけるまですべてのオプションをループすることです。http://selenium.googlecode.com/svn/trunk/docs/api/rb/selenium/webdriver/driver.html#execute_script-crass_methodselectedindexを設定する方法。
これを行う他の方法はありますか? Selenium 2.0/WebDriverのJava APIで: http://seleniumhq.org/docs/09_webdriver.htmlこれを行う例があります
Select select = new Select(driver.findElement(By.xpath("//select")));
select.deselectAll();
select.selectByVisibleText("Edam");
私が何かが足りないのでなければ、Rubyバージョンにはこの機能があるようには見えません。どんな助けも感謝します。
解決
上記のどれも機能しないことに注意してください。 Element#select
と Element#toggle
非推奨されています。あなたは次のようなことをする必要があります:
my_select.click
my_select.find_elements( :tag_name => "option" ).find do |option|
option.text == value
end.click
他のヒント
ここでの完全な開示:Rubyの実用的な知識はまったくありません。
しかし、私はセレンがかなり上手なので、私は助けることができると思います。あなたが探しているのはだと思います select
方法。 Rubyが他のドライバーのようなものである場合、選択方法を使用して選択されたオプションのいずれかを伝えることができます。
pseudocode/javaの用語では、このようなものになります
WebElement select = driver.findElement(By.name("select"));
List<WebElement> options = select.findElements(By.tagName("option"));
for(WebElement option : options){
if(option.getText().equals("Name you want")) {
option.click();
break;
}
}
上記の選択オブジェクトは、実際には特別なサポートパッケージにあります。それは現時点でJavaと.NETのためだけに存在します(2011年1月)
これがどのようなバージョンのセレニウムが生まれたのかわかりませんが、PnewhookがSelenium 2.20で言及した選択クラスがあるように見えます。
option = Selenium::WebDriver::Support::Select.new(@driver.find_element(:xpath => "//select"))
option.select_by(:text, "Edam")
Pnewhookはそれを手に入れましたが、私はここにRubyバージョンを投稿したいので、誰もがそれを見ることができます:
require "selenium-webdriver"
driver = Selenium::WebDriver.for :firefox
driver.manage.timeouts.implicit_wait = 10
driver.get "https://example.com"
country_select = driver.find_element(:id=> "address_country")
options = country_select.find_elements(:tag_name=>"option")
options.each do |el|
if (el.attributes("value") == "USA")
el.click()
break
end
end
使用できます xpath ループを避けるために:
String nameYouWant = "Name you want";
WebElement select = driver.findElement(By.id(id));
WebElement option =
select.findElement(By.xpath("//option[contains(text(),'" + nameYouWant + "')]"));
option.click();
また
WebElement option =
select.findElement(By.xpath("//option[text()='" + nameYouWant + "']"));
require "selenium-webdriver"
webdriver = Selenium::WebDriver.for :firefox
driver.navigate.to url
dropdown = webdriver.find_element(:id,dropdownid)
return nil if dropdown.nil?
selected = dropdown.find_elements(:tag_name,"option").detect { |option| option.attribute('text').eql? value}
if selected.nil? then
puts "Can't find value in dropdown list"
else
selected.click
end
私の場合、これは1つの作業サンプルです。
WebDriver(RC3)の最新バージョンの場合、setSelected()の代わりに「click()」を使用する必要があります。また、option.getText()。equals( "name you want")option.getText()== "name want you want" in javaの代わりに使用する必要があります。
<!-- language: lang-java -->
WebElement select = driver.findElement(By.name("select"));
List<WebElement> options = select.findElements(By.tagName("option"));
for(WebElement option : options){
if(option.getText().equals("Name you want"){
option.click();
break;
}
}
例を備えたルビーコード:
require "selenium-webdriver"
driver = Selenium::WebDriver.for :ie
driver.navigate.to "http://google.com"
a=driver.find_element(:link,'Advanced search')
a.click
a=driver.find_element(:name,'num')
options=a.find_elements(:tag_name=>"option")
options.each do |g|
if g.text == "20 results"
g.click
break
end
end
私が見つけた最も簡単な方法は、次のとおりです。
select_elem.find_element(:css、 "option [value = 'some_value'])。
#SELECT FROM DROPDOWN IN RUBY USING SELENIUM WEBDRIVER
#AUTHOR:AYAN DATE:14 June 2012
require "rubygems"
require "selenium-webdriver"
begin
@driver = Selenium::WebDriver.for :firefox
@base_url = "http://www.yoururl.com"
@driver.manage.timeouts.implicit_wait = 30
@driver.get "http://www.yoursite.com"
#select Urugway as Country
Selenium::WebDriver::Support::Select.new(@driver.find_element(:id, "country")).select_by(:text, "Uruguay")
rescue Exception => e
puts e
@driver.quit
end