Jag*_*tra 8 selenium python-3.x
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()
Run Code Online (Sandbox Code Playgroud)
它只是在页面加载后退出。等待根本没有效果!
演示:https : //www.youtube.com/watch?v=GocfsDZFqk8&feature=youtu.be
任何帮助将不胜感激。
And*_*kov 10
如果您想暂停 10 秒,请使用time.sleep()
:
import time
time.sleep(10) # take a pause 10 seconds
Run Code Online (Sandbox Code Playgroud)
注意: 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")))
Run Code Online (Sandbox Code Playgroud)
但它不会完全等待 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)
"""
...
Run Code Online (Sandbox Code Playgroud)
driver.implicitly_wait(10)
也用于等待元素,而不是暂停脚本。
PS:使用WebDriverWait
而不是硬暂停总是一个好习惯,因为WebDriverWait
你的测试会更快,因为你不必等待整个时间,而只是直到expected_conditions
你满意为止。据我了解,您目前只是在玩,但为了将来WebDriverWait
更好地使用。
小智 5
至少对于 Python 和 Chrome 驱动程序,我的经验是,即使使用 WebDriverWait,您仍然需要使用 time.sleep 才能可靠地工作。使用implicitly_wait不起作用。我需要在每次操作后放置 time.sleep(1) ,否则有时事情不会启动。
归档时间: |
|
查看次数: |
6642 次 |
最近记录: |