在循环期间输入信息到textarea或从下拉列表中的文本值中选择? (红宝石/的Watir)

时间:2021-12-04 19:40:26

At the moment I have:

目前我有:

def addInfoToStory(idOfStory, *stories)

i = -1
numOfInputs = 9
  while i < numOfInputs
  stories.each.with_index do |story|
  $log.puts "i="+"#{i}"
  $log.puts story[i]
  $log.puts "i2="+"#{i}"
  @browser.div(id: "#{idOfStory}_firstCol").div(class: "tDetEntry", index: i+1).div.double_click
    if @browser.div(id: "#{idOfStory}_firstCol").div(class: "tDetEntry", index: (i+=1)).div(class: "formLib1").text_field(:id, "input").set(story[i])
       sleep 2
       @browser.send_keys(:tab)
    else @browser.div(id: "#{idOfStory}_firstCol").div(class: "tDetEntry", index: (i+=1)).div(class: "formLib1").select_list(:id, "select").set(story[i])
       sleep 2
       @browser.send_keys(:tab)
    end
  end 
 end
end

The majority of information are textarea boxes which work fine, but when I get to a dropdown, the value won't change. Any ideas why?

大多数信息都是textarea盒子,工作正常,但是当我进入下拉列表时,值不会改变。有什么想法吗?

2 个解决方案

#1


3  

The easiest solution would be to have the if statement check if there is a text field or a select list present.

最简单的解决方案是让if语句检查是否存在文本字段或选择列表。

div_container = @browser.div(id: "#{idOfStory}_firstCol").div(class: "tDetEntry", index: (i+=1)).div(class: "formLib1")
if div_container.text_field(:id, "input").present?
    div_container.text_field(:id, "input").set(story[i])
elsif  div_container.select_list(:id, "select").present?
    div_container.select_list(:id, "select").select(story[i])
end
sleep 2
@browser.send_keys(:tab)

Also note that for select_lists, it has to be .select instead of .set.

另请注意,对于select_lists,它必须是.select而不是.set。

#2


0  

Required

storyFields.each_index {|i|
div_container = @browser.div(id: "#{idOfStory}_firstCol").div(class: "tDetEntry", index:     (i+=1)).div(class: "formLib1")
if div_container.text_field(:id, "input").present?
   div_container.text_field(:id, "input").set(story[i])
elsif  div_container.select_list(:id, "select").present?
       div_container.select_list(:id, "select").select(story[i])
end
}

Previous answer worked to some extent but didn't loop correctly.

以前的答案在某种程度上起作用,但没有正确循环。

#1


3  

The easiest solution would be to have the if statement check if there is a text field or a select list present.

最简单的解决方案是让if语句检查是否存在文本字段或选择列表。

div_container = @browser.div(id: "#{idOfStory}_firstCol").div(class: "tDetEntry", index: (i+=1)).div(class: "formLib1")
if div_container.text_field(:id, "input").present?
    div_container.text_field(:id, "input").set(story[i])
elsif  div_container.select_list(:id, "select").present?
    div_container.select_list(:id, "select").select(story[i])
end
sleep 2
@browser.send_keys(:tab)

Also note that for select_lists, it has to be .select instead of .set.

另请注意,对于select_lists,它必须是.select而不是.set。

#2


0  

Required

storyFields.each_index {|i|
div_container = @browser.div(id: "#{idOfStory}_firstCol").div(class: "tDetEntry", index:     (i+=1)).div(class: "formLib1")
if div_container.text_field(:id, "input").present?
   div_container.text_field(:id, "input").set(story[i])
elsif  div_container.select_list(:id, "select").present?
       div_container.select_list(:id, "select").select(story[i])
end
}

Previous answer worked to some extent but didn't loop correctly.

以前的答案在某种程度上起作用,但没有正确循环。