简体   繁体   English

Selenium Python如何确定动态下拉列表是否已完全加载?

[英]Selenium Python How to determine if a dynamic drop-down list is fully loaded?

Example: To wait for 示例:等待

<select id="myselect"></select>

to be populated with 被填充

<option value="123">One-two-three</option>
<option value="abc">ABC</option>
             ...

I asked this kind of question before, however, this time, I have a more complicated situation: 我之前问过这种问题,但是这一次,我遇到了一个更加复杂的情况:

I don't know what their option values are; 我不知道他们的期权价值是多少。 I don't know how many of them will be populated; 我不知道会有多少人居住;

For this kind of situation, is it time.sleep() the best approach? 对于这种情况, time.sleep()是最好的方法吗?

To be sure, you'd need some sort of known information, such as what the last option would be or how many in total you expect. 可以肯定的是,您需要某种已知的信息,例如最后一个选择是什么,或者期望总数。 Otherwise, you are essentially making an assumption that it's done. 否则,您实际上是在假设已完成。 If some sort of delay occurs that causes an element to appear late, you might miss it. 如果发生某种延迟,导致某个元素显示晚,您可能会错过它。

You could count the number, sleep, then count the number again and see if there's a change. 您可以数数,睡觉,然后再数数,看看是否有变化。 If not, that would indicate you're done, but again, delays could still occur. 如果没有,则表明您已完成,但同样,延迟仍然可能发生。

I do not know Python (I use Java), so I cannot help you with the syntax. 我不了解Python(我使用Java),因此在语法方面无法为您提供帮助。

You can use FluentWait to count the number of options twice, several milliseconds apart. 您可以使用FluentWait两次计数选项的数量,间隔为几毫秒。 If the counts match, you're good to go. 如果计数匹配,那您就很好了。

Another option may be available to you if your web server has a public API that would retrieve the full list of options that should appear in the dropdown. 如果您的Web服务器具有一个公共API,该公共API可以检索下拉菜单中应显示的选项的完整列表,那么您可以使用另一个选项。 You would then be able to make an api request to get the list of options that should display in the menu without having to actually look at the menu. 然后,您可以发出api请求,以获取应该显示在菜单中的选项列表,而无需实际查看菜单。

your test of each menu option can proceed, waiting for each target menu item as necessary. 您可以对每个菜单选项进行测试,并根据需要等待每个目标菜单项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM