English 中文(简体)
我如何选择使用Lelenium网络司机(月2日)在废墟中的客户
原标题:How do I set an option as selected using Selenium WebDriver (selenium 2.0) client in ruby

I am trying to get familiar with the new ruby selenium-webdriver as it appears more intuitive mostly than the previous version of selenium and the ruby driver that went with it. Also, i had trouble getting the old selenium to work with ruby 1.9.1 in windows so I thought i d look for an alternative. So far i ve done this with my script:

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

So basically I m logging into my site and trying to add an education entry to my user profile.. I have a reference to a select box with options (in the country_select variable) and now i want to select an option with a given value.. I don t see how to do this in the new client.. The only thing I can think of doing is looping through all the options until I find the one I want, and then call execute_script: http://selenium.googlecode.com/svn/trunk/docs/api/rb/Selenium/WebDriver/Driver.html#execute_script-class_method method to set the selectedIndex.

Is there any other way to do this? In the java api for selenium 2.0/webdriver here: http://seleniumhq.org/docs/09_webdriver.html there is an example of doing this

Select select = new Select(driver.findElement(By.xpath("//select")));
select.deselectAll();
select.selectByVisibleText("Edam");

It doesn t appear that the ruby version has this feature though unless I m missing something. Any help would be appreciated.

最佳回答

请注意,上述任何内容都不会再奏效。 <代码>Element#select and Element#toggle have been deprecated. 你们需要做这样的事情:

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

此处全面披露: 我完全不知道Ruby。

然而,我对硫化物非常好,因此我认为我可以提供帮助。 我想,你回顾的是select。 方法。 如果废墟与其他司机一样,你可以采用选择的方法来说明所选择的选项之一。

在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和 。 本期净额(Jan 2011年)

pnewhookt, 但我要在这里作废,以便每个人都能看到:

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()”而不是“板块”。 另外,还应使用“你想要”等办法,而不是选择。

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




相关问题
Ruby parser in Java

The project I m doing is written in Java and parsers source code files. (Java src up to now). Now I d like to enable parsing Ruby code as well. Therefore I am looking for a parser in Java that parses ...

rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

multiple ruby extension modules under one directory

Can sources for discrete ruby extension modules live in the same directory, controlled by the same extconf.rb script? Background: I ve a project with two extension modules, foo.so and bar.so which ...

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

热门标签