我正在使用clj-webdriver测试网站表单。如果表单字段具有相同的类,我想知道如何使用(input-text)
函数。
从它给出的(input-text)
定义“将字符串s
键入到使用查询q
找到的第一个表单元素中”。因为每个字段都有相同的类,当我给出时,
(input-text ".class")
它只填充第一个字段。有没有办法区分同一个类的所有字段?
表单的字段只有class
和type
作为选择器。
谢谢
答案 0 :(得分:3)
input-text
仅填充第一场比赛。
使用quick-fill
填写所有内容。
E.g,:
(quick-fill {".class" "s"})
/编辑
你说"对于同一类的2个字段,我必须输入2和3作为值。并且如果该类是"对象对象完成"我可以将课程视为" .object"。我不确定你对后者的意思,但我明白你想要为不同的元素添加不同的值。
如果您想查找特定元素,可以使用find-elements
。这些将返回elements:
(find-elements {:class ".class"})
这将找到类" .class"的所有元素。按顺序显示在页面上。
如果集合存储在变量文本中,则可以基于索引通过input-text
添加到每个元素。因此,例如,如果要向它们添加增加的索引,可以使用map-indexed
将每个值的索引添加到元素中,如下所示(调用doall
来遍历延迟序列中的每个元素 - 只有在访问元素并且doall
发生这种情况时才会进行函数调用):
(defn fill!
"Fills all elements with class class with increasing numbers."
[class]
(let [elements (find-elements {:class class})]
(doall
(map-indexed (fn [index element]
(input-text element (str index)))
elements))))
此函数的调用类似于(fill! ".class")
。
希望这有帮助。
答案 1 :(得分:0)
您应该使用(find-elements [webelement by])
函数,该函数会返回“webelements matching a given
by”列表。
从项目文档中可以找到https://github.com/semperos/clj-webdriver/wiki/Introduction%3A-Taxi,例如:
(defn css-finder
"Given a CSS query `q`, return a lazy seq of the elements found by calling `find-elements` with `by-css`. If `q` is an `Element`, it is returned unchanged."
[q]
(if (element? q)
q
(core/find-elements *driver* {:css q})))