如何使用ruby中的Selenium WebDriver(selenium 2.0)客户端设置选项

Mat*_*lfe 31 ruby select selenium-webdriver

我试图熟悉新的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")
Run Code Online (Sandbox Code Playgroud)

所以基本上我正在登录我的网站,并尝试在我的用户配置文件中添加一个教育条目.我有一个带选项的选择框的引用(在country_select变量中),现在我想选择一个给定值的选项..我没有看到如何在新客户端中执行此操作.我唯一能想到的是循环遍历所有选项,直到找到我想要的那个,然后调用execute_script: http:// selenium. googlecode.com/svn/trunk/docs/api/rb/Selenium/WebDriver/Driver.html#execute_script-class_method 方法设置selectedIndex.

有没有其他方法可以做到这一点?在这里的java api for selenium 2.0/webdriver:http://seleniumhq.org/docs/09_webdriver.html 有一个这样做的例子

Select select = new Select(driver.findElement(By.xpath("//select")));
select.deselectAll();
select.selectByVisibleText("Edam");
Run Code Online (Sandbox Code Playgroud)

除非我遗漏了某些东西,否则ruby版本似乎没有这个功能.任何帮助,将不胜感激.

pne*_*ook 35

完全披露:我完全没有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;
        }
    }
Run Code Online (Sandbox Code Playgroud)

您上面的Select对象实际上是一个特殊的Support包.它目前只存在于Java和.Net(2011年1月)

  • option.setSelected(); 已弃用并从2.2或2.3版本中删除.使用option.click(); 代替 (4认同)

Tys*_*son 18

请注意,上述任何一项都不再适用.Element#selectElement#toggle已被弃用.你需要做一些事情:

my_select.click
my_select.find_elements( :tag_name => "option" ).find do |option|
  option.text == value
end.click
Run Code Online (Sandbox Code Playgroud)


gru*_*rus 13

我不知道Selenium的版本是什么版本,但看起来像Selenium 2.20中提到的select类是pnewhook

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")
Run Code Online (Sandbox Code Playgroud)

  • 这是最好的答案!没有必要自己迭代元素,更不用说在代码中反复查看:tag_name =>'option'了.专业提示:为了方便访问Select类(即Select.new而不是Selenium :: WebDriver :: Support :: Select.new),请使用:include Selenium :: WebDriver :: Support (3认同)
  • 如果你想按值选择,你也可以这样做:`option.select_by(:value,"edam")` (3认同)

Mat*_*lfe 7

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
Run Code Online (Sandbox Code Playgroud)


Dav*_*oni 6

您可以使用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();
Run Code Online (Sandbox Code Playgroud)

要么

WebElement option = 
  select.findElement(By.xpath("//option[text()='" + nameYouWant + "']"));
Run Code Online (Sandbox Code Playgroud)