<p>试图将<code>find_element_by_id</code>传递给<code>presence_of_element_located</code>的构造函数(如<a href="https://stackoverflow.com/a/26567563/3657941">accepted answer</a>所示)导致<code>NoSuchElementException</code>被引发。我不得不使用<a href="https://stackoverflow.com/users/3111047/fragles">fragles</a>'<a href="https://stackoverflow.com/questions/26566799/selenium-python-how-to-wait-until-the-page-is-loaded#comment52897826_26567563">comment</a>中的语法:</p>
<pre><code>from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
driver = webdriver.Firefox()
driver.get('url')
timeout = 5
try:
element_present = EC.presence_of_element_located((By.ID, 'element_id'))
WebDriverWait(driver, timeout).until(element_present)
except TimeoutException:
print "Timed out waiting for page to load"
</code></pre>
<p>这与<a href="http://selenium-python.readthedocs.io/waits.html#explicit-waits" rel="noreferrer">example in the documentation</a>匹配。这是指向<a href="http://selenium-python.readthedocs.io/api.html?highlight=#module-selenium.webdriver.common.by" rel="noreferrer">documentation for By</a>的链接。</p>