我正在使用 selenium,在 python 中编写脚本,以测试具有 gwt-SuggestBox 的网页:
<div id="streamSuggestBox">
<table> <tbody><tr> <td> <div class="gwt-Label errorText">Stream:</div> </td>
<td> <div><input type="text" class="gwt-SuggestBox"></div> </td>
</tr> </tbody></table>
</div>
selenium 可以找到 div 和输入小部件:
(Pdb) sugInput = self.driver.find_element_by_id("streamSuggestBox").find_element_by_tag_name("input")
(Pdb) p sugInput.tag_name
u'input'
如果输入小部件中有文本,sugInput.clear() 会清除它。
问题是 sugInput.send_keys("s") 不起作用——输入字段中没有显示任何内容,也没有提出建议。我应该如何在我的 gwt-SuggestBox 中输入数据?
请您参考如下方法:
您可以尝试直接设置value 属性。抱歉,下面的代码是用 C# 编写的,但概念在 Python 中应该是相同的:
string script = "arguments[0].setAttribute('value', argument[1])";
IWebElement suggestionBox = //find it somehow...
driver.ExecuteScript(script, suggestionBox , "I typed this!");
要点是使用javascript直接修改元素的value属性。 Selenium 不在他们的 API 中对此提供直接支持,因为他们不相信用户会直接修改值。然而,对于“困难”的对象,有时有必要作为一种解决方法。
