我的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"]
答案 0 :(得分:1)
假设您的真正目标是根据文本设置复选框(即您实际上不需要名称数组),则可以使用xpath选择器查找紧跟所需文本的复选框元素: / p>
#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
答案 1 :(得分: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|