如何在selenium IDE的帮助下从select2中选择选项

时间:2016-04-05 06:53:30

标签: ruby-on-rails selenium selenium-webdriver selenium-ide select2

我正在使用一些测试用例场景,其中我的表单中有多个select2选项。我试图用来自我的服务器(AJAX请求)的值填写select2框。 我尝试过使用硒记录仪。哪个不是很好的帮助。它并不了解select2的工作原理。

我将命令放在下面。

ref1

最后一行是抛出以下错误

@driver.get(@base_url + "/app/customers")
@driver.find_element(:link, "Add Customer").click
@driver.find_element(:name, "customer[name]").click
@driver.find_element(:name, "customer[name]").clear
@driver.find_element(:name, "customer[name]").send_keys "Chinmay"
Selenium::WebDriver::Support::Select.new(@driver.find_element(:xpath, "//*[@id=\"page-content\"]/div/div/div/div/form/div[4]/div[1]/div[4]/select")).select_by(:text, "label=Primary")

我的HTML代码如下

[error] Option with label 'Primary' not found

1 个答案:

答案 0 :(得分:1)

  1. 使用selenium IDE创建代码不是一个好主意

  2. 请考虑水豚或watir-webdriver。你会喜欢它。

  3. 尝试做类似

    的事情
    wait = Selenium::WebDriver::Wait.new(:timeout => 10) #Let's wait until Ajax-request will be done
    wait.until { @driver.find_element(xpath: "//option[@value='primary']") } #That's the waiting
    dropdown = @driver.find_element(xpath: "//select[@data-name='address_types']") #Nice way to find your dropdown
    select_list = Selenium::WebDriver::Support::Select.new(dropdown) #Let's create select_list out of dropdown-element
    select_list.select_by(:text, 'Primary') #Finally.