簡體   English   中英

如何在 ruby​​ 中使用 Selenium WebDriver (selenium 2.0) 客戶端將選項設置為選定的選項

[英]How do I set an option as selected using Selenium WebDriver (selenium 2.0) client in ruby

我正在嘗試熟悉新的 ruby​​ selenium-webdriver,因為它看起來比以前版本的 selenium 和隨附的 ruby​​ 驅動程序更直觀。 另外,我無法讓舊的 selenium 在 windows 中與 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 變量中),現在我想選擇一個具有給定值的選項.. 我不知道如何在新客戶端中執行此操作.. 我唯一能想到的就是遍歷所有選項,直到找到我想要的選項,然后調用 execute_script: http://selenium。 googlecode.com/svn/trunk/docs/api/rb/Selenium/WebDriver/Driver.html#execute_script-class_method方法來設置 selectedIndex。

有沒有其他方法可以做到這一點? 在 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​​ 版本似乎沒有這個功能。 任何幫助將不勝感激。

在這里完全披露:我完全沒有 Ruby 的工作知識。

但是,我對 Selenium 非常擅長,所以我想我可以提供幫助。 我認為您正在尋找的是select方法。 如果 ruby​​ 與其他驅動程序類似,您可以使用 select 方法告訴它被選中的選項之一。

在偽代碼/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;
        }
    }

您上面的 Select 對象實際上位於一個特殊的支持包中。 它目前僅適用於 Java 和 .Net(2011 年 1 月)

請注意,以上方法都不再適用。 Element#selectElement#toggle已被棄用。 您需要執行以下操作:

my_select.click
my_select.find_elements( :tag_name => "option" ).find do |option|
  option.text == value
end.click

我不知道這是什么版本的 Selenium,但看起來 Selenium 2.20 中提到了 pnewhook 的 Select 類

http://selenium.googlecode.com/svn-history/r15117/trunk/docs/api/rb/Selenium/WebDriver/Support/Select.html

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

就我而言,這只是一個工作示例。

對於最新版本的 Webdriver (RC3),您應該使用“click()”而不是 setSelected()。 還應使用 option.getText().equals("Name you want") 而不是 option.getText()=="Name you want" 在 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;
    }
}

Ruby 代碼示例:

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 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

我找到的最簡單的方法是:

select_elem.find_element(:css, "option[value='some_value']").click

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM