2

我确实有以下内容HTML

<div id="filtersetedit_fieldNames" title="Please first select a list to filter!">
  <input value="5418630" name="NameID" type="checkbox"> John Das
  <input value="6360899" name="NameID" type="checkbox"> Ram Roy 
  <input value="9556609" name="NameID" type="checkbox"> Smith
  <input value="20156687" name="NameID" type="checkbox"> Paul
</div>

现在我必须使用 selenium-webdriver 单击相应的复选框。但我试图将值提取为John Das,Ram roy etc. 这是因为只是为了让索引号点击复选框。但我的代码根本不起作用。

CODE

driver.find_elements(:id,"filtersetedit_fieldNames").each do |x|
  puts x.text
  #puts index
  break if x.text == "LocationAttributes:Currency Type"
  index = index + 1
end

driver.find_elements(:name, "candidateFieldIds")[index].click

但是这些值被打印为一个字符串"John Das Ram Roy Smith Paul",我很困惑如何找到正确的索引,以便我可以在我的check box命中代码中使用它。

我实际上正在寻找创建一个数组类型的东西说它应该像["John Das","Ram Roy","Smith","Paul"]

4

2 回答 2

1

假设您的真正目标是根据文本设置复选框(即您实际上并不需要名称数组),您可以使用 xpath 选择器查找紧跟所需文本的复选框元素:

#The text of the checkbox you want to select
text_of_target_checkbox = 'Ram Roy'

#The div containing the checkboxes
checkbox_div = driver.find_element(:id => 'filtersetedit_fieldNames')

#Get the checkbox that has the specified text immediately after it
e = checkbox_div.find_element(:xpath => "./input            
    [following-sibling::text()
        [
            position() = 1 and 
            normalize-space(.)='#{text_of_target_checkbox}'
        ]
    ]")

#Click the checkbox
e.click
于 2013-02-06T17:04:21.910 回答
0

也许:

driver.find_elements(
  :xpath,
  "//div[contains(@id,'filtersetedit_fieldNames')]/input"
) do |x|

应该:

driver.find_elements(
  :xpath,
  "//div[contains(@id,'filtersetedit_fieldNames')]/input"
).each do |x|
于 2013-02-06T03:18:24.770 回答