简体   繁体   English

watir-webdriver的红宝石拆分数组

[英]ruby splitting array for watir-webdriver

is there a way to split an array in half and then access each half 1 at a time like array[0] then array[1] then do something with each half? 有没有一种方法可以将数组分成两半,然后一次访问每个半1,如array [0]然后array [1]然后对每个半做些什么?

I am populating the array for a select list and want to run through the list half at a time, due to website timing out. 由于网站超时,我正在为选择列表填充数组,并且希望一次遍历整个列表。

When selecting all of the accounts at once, either manually or through ruby watir-webdriver script website times out. 一次或通过ruby watir-webdriver脚本网站一次选择所有帐户时,网站超时。

This is how I was doing it. 这就是我的做法。

b.select_list(:id => 'ctl00_ctl00_MainContent_ContentPlaceHolder2_ListBoxAllAcct').options.each { |option| option.select }
b.button(:id => 'ctl00_ctl00_MainContent_ContentPlaceHolder2_LinkButtonAddAcct').click

Then I tried one at a time but takes to long and creates to many file downloads, on for each option value. 然后我一次尝试了一次,但是花了很长时间,并且为每个选项值创建了许多文件下载。

elems = Array.new
values = Array.new
elems = b.select_list(:id => "ctl00_ctl00_MainContent_ContentPlaceHolder2_ListBoxAllAcct").options
0.upto(elems.length - 1) do |i|
  values.push elems[i].text
end

0.upto(values.length - 1) do |i|
  puts values[i]
  b.select_list(:id =>   'ctl00_ctl00_MainContent_ContentPlaceHolder2_ListBoxAllAcct').select values[i]
  b.button(:id => 'ctl00_ctl00_MainContent_ContentPlaceHolder2_LinkButtonAddAcct').click
end

I am simply trying to find a way to select and process the first half the list then move one to the second set of the list. 我只是想找到一种方法来选择和处理列表的前半部分,然后将其移至列表的第二组。 Any suggestions or help is greatly appreciated. 任何建议或帮助,我们将不胜感激。

Try this: 尝试这个:

values = b.select_list(id: 
  'ctl00_ctl00_MainContent_ContentPlaceHolder2_ListBoxAllAcct').options.map(&:text)

bulk_size = values.length / 2

values.each_slice(bulk_size) do |bulk_values|
  b.select_list(id: 
  'ctl00_ctl00_MainContent_ContentPlaceHolder2_ListBoxAllAcct').options.select do |option|
    bulk_values.include? option.text
  end.each do |option|
    option.select
  end
  b.button(id:
    'ctl00_ctl00_MainContent_ContentPlaceHolder2_LinkButtonAddAcct').click
end

If you see that half is still too much of a bulk, you can simply play with bulk_size , setting it to 10 for example, instead of values.length / 2 如果您看到一半仍然太大了,您可以简单地使用bulk_size ,例如将其设置为10 ,而不是values.length / 2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM