硒等待元素
如何在 Python 中为 Selenium 编写等待只有类标识符的表的函数?我正在花很长时间学习使用 Selenium 的 Python webdriver 函数。
How do I write the function for Selenium to wait for a table with just a class identifier in Python? I'm having a devil of a time learning to use Selenium's Python webdriver functions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
来自 Selenium 文档 PDF :
From the Selenium Documentation PDF :
Selenium 2 的 Python 绑定有一个名为 Expected_conditions.py 的新支持类,用于执行各种操作,例如测试元素是否可见。 可在此处获取。
注意: 截至 2012 年 10 月 12 日,上述文件已在主干中,但尚未在最新的下载版本中(仍为 2.25)。目前,在新的 Selenium 版本发布之前,您可以暂时将此文件保存在本地并将其包含在导入中,就像我在下面所做的那样。
为了让生活变得更简单,您可以将其中一些预期条件方法与 Selenium
wait Until
逻辑结合起来,以创建一些非常方便的功能,类似于 Selenium 1 中提供的功能。例如,我将这个进入我的名为 SeleniumTest 的基类,我的所有 Selenium 测试类都扩展了它:然后您可以在测试中轻松使用它们,如下所示:
Selenium 2's Python bindings have a new support class called expected_conditions.py for doing all sorts of things like testing if an element is visible. It's available here.
NOTE: the above file is in the trunk as of Oct 12, 2012, but not yet in the latest download which is still 2.25. For the time being until a new Selenium version is released, you can just save this file locally for now and include it in your imports like I've done below.
To make life a little simpler, you can combine some of these expected condition methods with the Selenium
wait until
logic to make some very handy functions similar to what was available in Selenium 1. For example, I put this into my base class called SeleniumTest which all of my Selenium test classes extend:You can then use these easily in your tests like so:
我使用以下方法获得了良好的体验:
第一个非常明显 - 只需等待几秒钟即可完成某些操作。
对于我的所有 Selenium 脚本,当我在笔记本电脑上运行它们时,几秒钟(范围从 1 到 3 秒)的 sleep() 就可以工作,但在我的服务器上等待的时间范围更广,所以我也使用implicitly_wait()。我通常使用implicitly_wait(30),这确实足够了。
I have made good experiences using:
The first one is pretty obvious - just wait a few seconds for some stuff.
For all my Selenium Scripts the sleep() with a few seconds (range from 1 to 3) works when I run them on my laptop, but on my Server the time to wait has a wider range, so I use implicitly_wait() too. I usually use implicitly_wait(30), which is really enough.
我为 python 的 wait_for_condition 实现了以下内容,因为 python selenium 驱动程序不支持此函数。
用作
等待 ExtJS Ajax 调用未挂起:
设置 Javascript 变量
等。
I implemented the following for python for wait_for_condition since the python selenium driver does not support this function.
to be used as
Wait that an ExtJS Ajax call is not pending:
A Javascript variable is set
etc.
您始终可以在循环中使用短暂睡眠并将其传递给您的元素 id:
You could always use a short sleep in a loop and pass it your element id:
将
Wait Until Page Contains Element
与正确的 XPath 定位器结合使用。例如,给定以下 HTML:... 您可以输入以下关键字:
除非我错过了某些内容,否则无需为此创建新函数。
Use
Wait Until Page Contains Element
with the proper XPath locator. For example, given the following HTML:... you can enter the following keyword:
Unless I missed something, there is no need to create a new function for this.
如果这有帮助...
在 Selenium IDE 中,我添加了...
命令:waitForElementPresent
Target: //table[@class='pln']
然后我做了 File>Export TestCase As Python2(Web Driver),它给了我这个...
In case this helps ...
In the Selenium IDE, I added ...
Command: waitForElementPresent
Target: //table[@class='pln']
Then I did File>Export TestCase As Python2(Web Driver), and it gave me this ...
更简单的解决方案:
easier solution:
希望这有帮助
Hopefully this helps
如果我不了解 selenium 命令,我会在 firefox 中使用 selenium web idea RC。您可以在组合框中选择并添加命令,完成测试用例后可以将测试代码导出为不同的语言。比如java、ruby、phyton、C#等。
If I don't know something about selenium command, I use selenium web idea RC with firefox. You can choose and add command in the combobox and when finish your test case after you can export the test code different language. like java, ruby, phyton, C#, etc..
您可以将此功能修改为所有类型的元素。下面的仅针对类元素:
其中“driver”是驱动程序,“element_name”是您要查找的类名称,“sec”是您愿意等待的最大秒数。
You can modify this function to all type of elements. The one below is just for the class element:
Where "driver" is the driver, "element_name" is the class name you are looking for, and "sec" is the maximum amount of seconds you are willing to wait.
我找到了一种使用自定义函数更简单的方法来构建它,该函数本质上是递归的。
您可以根据您的情况将
find_element_by_id
替换为find_element_by_name
或find_element_by_tag_name
要求I found an easier way to build this using a custom function, which is recursive in nature
You can replace
find_element_by_id
withfind_element_by_name
orfind_element_by_tag_name
based on your requirement我希望这可能有所帮助:
我建议您阅读这篇文章让它更清楚。
I hope that's might help:
I recommend you to read this article to make it more clear.