擅长:python、mysql、java
<p>基本上,如果页面是由JavaScript生成的,那么就无法判断它何时“完成”了加载。</p>
<p>然而。我通常尝试检索一个页面元素,捕捉异常并继续尝试,直到达到超时。我还可以检查元素是否可见,而不仅仅是存在的。</p>
<p>必须有一些DOM元素的可见性可用于测试页是否已“完成”加载。在我的测试用例中,我通常有一个<code>wait_for_element_visibility</code>,和<code>wait_for_element_presence</code>函数。</p>
<pre><code>def wait_for_visibility(self, selector, timeout_seconds=10):
retries = timeout_seconds
while retries:
try:
element = self.get_via_css(selector)
if element.is_displayed():
return element
except (exceptions.NoSuchElementException,
exceptions.StaleElementReferenceException):
if retries <= 0:
raise
else:
pass
retries = retries - 1
time.sleep(pause_interval)
raise exceptions.ElementNotVisibleException(
"Element %s not visible despite waiting for %s seconds" % (
selector, timeout_seconds)
)
</code></pre>
<p><code>get_via_css</code>是我自己的函数之一,但我希望它非常清楚它在做什么。</p>