【问题标题】:How do I set an option as selected using Selenium WebDriver (selenium 2.0) client in ruby如何在 ruby​​ 中使用 Selenium WebDriver (selenium 2.0) 客户端设置选项
【发布时间】:2011-06-08 01:02:44
【问题描述】:

我正在尝试熟悉新的 ruby​​ selenium-webdriver,因为它看起来比之前版本的 selenium 和随附的 ruby​​ 驱动程序更直观。此外,我很难让旧硒在 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 select selenium-webdriver


    【解决方案1】:

    这里完全披露:我完全没有 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 对象实际上是在一个特殊的 Support 包中。目前仅适用于 Java 和 .Net(2011 年 1 月)

    【讨论】:

    • 这似乎不起作用,我试过 country_select = driver.find_element(:xpath, "//select[id='address_country']/option = 'Austria'") country_select.select 和那没有做任何事情。我认为select方法就像在javascript中调用select()(将焦点放在字段中并突出显示文本)
    • 您是否尝试过先获取 select 元素,然后遍历 WebElement 选项并在您想要的 WebElement 上调用 select 方法?
    • pnewhook 做对了。下面是等效的 Ruby 代码:gist.github.com/777516 如果 Element#select 不起作用,不妨试试 Element#toggle。如果您认为 API 级别太低,您可能需要查看 watir-webdriver gem 作为相同技术的替代 API。
    • 你不需要option.getText().equals("Name you want") 而不是option.getText()=="Name you want" 吗?
    • option.setSelected();已弃用并从 2.2 或 2.3 版本中删除。使用 option.click();而是
    【解决方案2】:

    请注意,以上所有方法都不再有效。 Element#select 和 Element#toggle 已被弃用。您需要执行以下操作:

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

    【讨论】:

      【解决方案3】:

      我不知道这是什么版本的Selenium,但看起来有pnewhook在Selenium 2.20中提到的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")
      

      【讨论】:

      • 这是最好的答案!无需自己遍历元素,更不用说在代码中一遍又一遍地查看 :tag_name => 'option' 了。专业提示:为了方便访问 Select 类(即 Select.new 而不是 Selenium::WebDriver::Support::Select.new),请使用:include Selenium::WebDriver::Support
      • 如果你想按值选择,你也可以这样做:option.select_by(:value, "edam")
      【解决方案4】:

      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
      

      【讨论】:

        【解决方案5】:

        您可以使用 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 + "']"));
        

        【讨论】:

          【解决方案6】:
          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
          

          就我而言,这只是一个工作示例。

          【讨论】:

            【解决方案7】:

            对于最新版本的 Webdriver (RC3),您应该使用“click()”而不是 setSelected()。在 JAVA 中也应该使用 option.getText().equals("Name you want") 而不是 option.getText()=="Name you want":

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

            【讨论】:

              【解决方案8】:

              带有示例的 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
              

              【讨论】:

                【解决方案9】:
                #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
                

                【讨论】:

                  【解决方案10】:

                  我找到的最简单的方法是:

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

                  【讨论】:

                    猜你喜欢
                    • 2012-01-24
                    • 2013-06-27
                    • 1970-01-01
                    • 2016-07-19
                    • 1970-01-01
                    • 2013-03-03
                    • 2013-03-27
                    • 1970-01-01
                    • 1970-01-01
                    相关资源
                    最近更新 更多