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



我正在处理一些测试用例场景,在这些场景中,我的表单中有多个select2选项。我正在尝试用来自我的服务器的值(AJAX请求)填充select2框。我试着用硒录音机。这不是一个好的帮助。它不理解select2的工作原理。

我把我的命令放在下面。

@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")

最后一行抛出以下错误

[error] Option with label 'Primary' not found

我的HTML代码如下

<select class="form-control amura-select2 select2-hidden-accessible" data-type="ds" data-name="address_types" data-default="only" style="width:100%;" required="required" name="customer[address_attributes][address_type]" aria-required="true" tabindex="-1" aria-hidden="true">
<option value="primary">Primary</option>
</select>
  1. 使用selenium IDE创建代码不是一个好主意

  2. 请考虑一下水豚或watir网络驱动程序。你会喜欢的。

  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. 
    

最新更新