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

前端 未结 10 1325
难免孤独
难免孤独 2020-12-14 00:46

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

相关标签:
10条回答
  • 2020-12-14 01:10

    I don't know what version Selenium this came about, but it looks like there is the Select class that pnewhook mentioned in Selenium 2.20

    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")
    
    0 讨论(0)
  • 2020-12-14 01:11

    For the latest version of Webdriver (RC3) you should use "click()" instead of setSelected(). Also option.getText().equals("Name you want") should be used instead of option.getText()=="Name 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;
        }
    }
    
    0 讨论(0)
  • 2020-12-14 01:14

    pnewhook got it but I'd like to post the ruby version here so everyone can see it:

    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
    
    0 讨论(0)
  • 2020-12-14 01:19
    #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
    
    0 讨论(0)
提交回复
热议问题