使用watir和ruby通过索引选择值

时间:2021-08-12 20:06:20

I have such code:

我有这样的代码:

total_terms = @driver.select_list(:name => 'ctl00$cp$cbRodzajUslugi').length
    if (1...5).include?(total_terms)
      @driver.select_list(:name => 'ctl00$cp$cbRodzajUslugi').option(:index, total_terms).select
    else
      @driver.select_list(:name => 'ctl00$cp$cbRodzajUslugi').option(:index, (total_terms-2)).select
    end

and I am trying to select some value via index. First, I calculate how long my select_list is, and then I select. But in the browser, I see that nothing is selected. What did I do wrong?

我试图通过索引选择一些值。首先,我计算我的select_list有多长,然后我选择。但是在浏览器中,我看到没有选择任何内容。我做错了什么?

1 个解决方案

#1


3  

Your code is probably throwing exceptions.

您的代码可能会抛出异常。

Select lists do not have a method length

选择列表没有方法长度

The line

@driver.select_list(:name => 'ctl00$cp$cbRodzajUslugi').length

is not valid since select lists do not have a method length. Assuming you want the number of options, need to add the options method to get a collection of options in the select list:

由于选择列表没有方法长度,因此无效。假设您需要多个选项,需要添加选项方法以获取选择列表中的选项集合:

@driver.select_list(:name => 'ctl00$cp$cbRodzajUslugi').options.length   

5 or less options selects non-existent option

5个或更少的选项选择不存在的选项

The line

if (1...5).include?(total_terms)
  @driver.select_list(:name => 'ctl00$cp$cbRodzajUslugi').option(:index, total_terms).select

will throw an exception due to there being nothing at the specified index. The :index locator is 0-based - ie 0 means the first option, 1 means the second option, etc. This means that when there are two options, you will try to select :index => 2, which does not exist. You need to subtract 1:

由于指定索引中没有任何内容,将抛出异常。 :index locator是从0开始的 - 即0表示第一个选项,1表示第二个选项,等等。这意味着当有两个选项时,您将尝试选择:index => 2,它不存在。你需要减去1:

if (1...5).include?(total_terms)
  @driver.select_list(:name => 'ctl00$cp$cbRodzajUslugi').option(:index, total_terms-1).select

#1


3  

Your code is probably throwing exceptions.

您的代码可能会抛出异常。

Select lists do not have a method length

选择列表没有方法长度

The line

@driver.select_list(:name => 'ctl00$cp$cbRodzajUslugi').length

is not valid since select lists do not have a method length. Assuming you want the number of options, need to add the options method to get a collection of options in the select list:

由于选择列表没有方法长度,因此无效。假设您需要多个选项,需要添加选项方法以获取选择列表中的选项集合:

@driver.select_list(:name => 'ctl00$cp$cbRodzajUslugi').options.length   

5 or less options selects non-existent option

5个或更少的选项选择不存在的选项

The line

if (1...5).include?(total_terms)
  @driver.select_list(:name => 'ctl00$cp$cbRodzajUslugi').option(:index, total_terms).select

will throw an exception due to there being nothing at the specified index. The :index locator is 0-based - ie 0 means the first option, 1 means the second option, etc. This means that when there are two options, you will try to select :index => 2, which does not exist. You need to subtract 1:

由于指定索引中没有任何内容,将抛出异常。 :index locator是从0开始的 - 即0表示第一个选项,1表示第二个选项,等等。这意味着当有两个选项时,您将尝试选择:index => 2,它不存在。你需要减去1:

if (1...5).include?(total_terms)
  @driver.select_list(:name => 'ctl00$cp$cbRodzajUslugi').option(:index, total_terms-1).select