from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
driver = webdriver.Firefox()
driver.get("https://google.com")
#driver.implicitly_wait(10)
WebDriverWait(driver,10)
print("waiting 10 sec")
driver.quit()
它只是在页面加载后退出。等待根本没有效果!
演示:https://www.youtube.com/watch?v=GocfsDZFqk8&feature=youtu.be
任何帮助都将不胜感激。
发布于 2018-07-14 01:03:27
如果您希望暂停10秒,请使用time.sleep()
import time
time.sleep(10) # take a pause 10 seconds
注意: WebDriverWait(driver,10)
不是那样工作的。相反,您可以这样使用它:
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
# this will wait at least 10 seconds until url will contain "your_url"
WebDriverWait(driver, 10).until(EC.url_contains(("your_url")))
但是它不会完全等待10秒,直到expected_conditions
满意为止。
还:作为源代码告诉我们:
def implicitly_wait(self, time_to_wait):
"""
Sets a sticky timeout to implicitly wait for an element to be found,
or a command to complete. This method only needs to be called one
time per session. To set the timeout for calls to
execute_async_script, see set_script_timeout.
:Args:
- time_to_wait: Amount of time to wait (in seconds)
:Usage:
driver.implicitly_wait(30)
"""
...
driver.implicitly_wait(10)
也用于等待元素,而不是暂停脚本。
PS:使用WebDriverWait
而不是硬暂停总是一种很好的实践,因为使用WebDriverWait
测试会更快,因为您不需要等待所有的时间,而只能等到expected_conditions
满意为止。正如我所理解的,目前您只是在四处游玩,但是对于未来,WebDriverWait
更适合使用。
发布于 2021-10-03 22:02:33
至少使用Python和Chrome驱动程序,我的经验是,即使使用WebDriverWait,您仍然需要使用time.sleep才能可靠地工作。使用implicitly_wait不起作用。我需要把time.sleep(1)放在每次手术之后,否则有时事情就不会启动。
发布于 2018-07-14 06:02:57
通过使用这个WebDriverWait(driver,10)
,您已经声明了显式等待。这只是声明,您根本没有使用显式等待。
为了使用显式等待,必须将上述代码绑定到EC (即Expected condition
)。
类似于:
wait = WebDriverWait(driver,10)
element = wait.until(EC.element_to_be_clickable((By.NAME, 'q')))
element.send_keys("Hi Google")
您可以引用此链接获取显式等待:显式等待
注意,time.sleep(10)
是显式等待的worst/extreme
类型,它将条件设置为等待的确切时间。有一些方便的方法可以帮助您编写只在需要时等待的代码。WebDriverWait与ExpectedCondition的结合是实现这一目标的一种方法。
https://stackoverflow.com/questions/51336849
复制相似问题