我试图熟悉新的Ruby Selenium-webdriver,因为它看起来比以前的Selenium和随之而来的Ruby驱动程序更直观。另外,我很难让旧的硒与Ruby 1.9.1在Windows中合作,所以我想我会寻找替代方案。到目前为止,我已经用脚本完成了此操作:

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/webdriver/driver.html#execute_script_script_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版本没有此功能。任何帮助,将不胜感激。

有帮助吗?

解决方案

请注意,以上都无法再起作用了。 Element#selectElement#toggle 已被弃用。您需要做类似的事情:

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

其他提示

在这里完全披露:我绝对没有Ruby的工作知识。

但是,我对硒很好,所以我认为我可以提供帮助。我认为您要寻找的是 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;
        }
    }

您上面拥有的选择对象实际上是在特殊支持包中。目前仅适用于Java和.net(2011年1月)

我不知道这是什么版本的硒,但是看来Pnewhook在Selenium 2.20中提到的选择类别

http://selenium.googlecode.com/svn-history/r15117/trunk/docs/rbi/rb/selenium/webdriver/support/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()。等于(应该使用“您想要的名称”)代替option.getText()== 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_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
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top