selenium:未找到嵌套的 div 元素
我对 selenium 有点陌生,并尝试使用 testng 和 eclipse 进行测试用例。我试图打开来自多个 ajax 调用的网页。有嵌套的 div 加载到表格内。一个 div 元素是一个搜索框。当我尝试使用测试用例输入搜索词时,它会出错“找不到元素”。我尝试了不同的方法来查找元素,但徒劳无功。我真的很感谢解决这个问题的方法。
谢谢, 〜萨拉特
I am kinda new to selenium and trying out test cases with testng with eclipse. The webpage i'm trying to open loads from multiple ajax calls. There are nested div's which load inside a table. one div element is a search box. When i'm trying to enter the search word using the test case it errors out 'element not found'. I tried out different ways of finding an element but in vain. I would really be thankful for ways to resolve this problem.
thanks,
~sharath
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议您使用 XPath,然后查找输入框就像将查询提交给 find_element_by_xpath 方法一样简单:“//input(@name='name')”
请参阅 W3school 了解完整的语法规范。
您可以使用一些工具来测试xpath,例如:Firebug。
我为此目的在我的博客上写了一篇文章。
I suggest that you use XPath and then finding an input box will be as simple as submitting this query to the find_element_by_xpath method: "//input(@name='name')"
Refer to W3school for complete syntax specification.
You could use some tools to test xpath, e.g.: Firebug.
I wrote an entry on my blog for this purpose.
如果不知道页面的结构等,就很难确切地知道发生了什么。
我可以建议的一件事是,在尝试使用它执行某些操作之前,您需要先等待所需的元素。
例如,您可以在尝试单击文本框等内容之前添加
waitForElementPresent
命令。考虑一下:
理想情况下,您需要 2 个命令:
然后
这些东西似乎很少包含在初始录制过程中。我经常必须进去并专门告诉 Selenium 等待某些元素。
Without knowing the structure of your page etc it's a little difficult to know exactly what's happening.
One thing I can sugest is that you do a wait for the element you require, before trying to do something with it.
For example, you may add a
waitForElementPresent
command before trying to click into something such as a text box.Consider:
Ideally you'll want to have 2 commands:
and then
These kinds of things rarely seem to be included as part of the initial recording process. I often have to go in and specifically tell Selenium to wait for certain elements.