"driver.implicitly_wait(20)" 和 "WebDriverWait(driver, 20)" 之间的区别
Difference between "driver.implicitly_wait(20)" and "WebDriverWait(driver, 20)"
我有两个与等待相关的问题。
首先,请解释一下这两种waiting方法有什么区别
WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.ID,
'twotabsearchtextbox')))
# and this
driver.implicitly_wait(20)
注意之前我写了个代码看看有什么不同,但是我并不清楚有什么不同
你写的代码
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import *
driver = webdriver.Chrome()
driver.set_window_position(-1200, 0)
driver.maximize_window()
driver.get("http://www.amazon.com/");
# driver.implicitly_wait(20)
WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.ID, 'twotabsearchtextbox')))
search = driver.find_element(By.ID, 'twotabsearchtextbox')
search.click()
search.send_keys('Laptop hp core i5')
第二个问题是我有时看到WebDriverWait方法赋值给一个变量,我的意思是这样的:
element = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.ID, 'twotabsearchtextbox'))))
有什么区别?
我以为 WebDriverWait 只是等待项目出现
和 driver.implicitly_wait(20)
函数
等待页面加载。
至于我是怎么回事,两种方法都在等待页面加载
隐式等待
通过 , the WebDriver instance polls the HTML DOM 尝试查找任何元素时持续一定时间。当网页上的某些元素不能立即使用并且需要一些时间加载时,这很有用。默认情况下,隐式等待元素出现是禁用的,需要在 per-session 的基础上手动启用。例如:
driver.implicitly_wait(10)
显式等待
allows our code to halt program execution, or freeze the thread, until the condition we pass it resolves. The condition is called with a certain frequency until the timeout of the wait is elapsed. This means that for as long as the condition returns a falsy value, it will keep trying and waiting. Thus Explicit waits allows us to wait for a condition to occur which inturn synchronises the state between the browser and its DOM Tree,以及 WebDriver 脚本。例如:
element = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.ID, 'twotabsearchtextbox')))
注意事项
不要混用隐式和显式等待。这样做会导致不可预测的等待时间。例如,设置 10 秒的隐式等待和 15[=36 秒的显式等待=] 秒可能导致在 20 秒后发生超时。
我有两个与等待相关的问题。 首先,请解释一下这两种waiting方法有什么区别
WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.ID,
'twotabsearchtextbox')))
# and this
driver.implicitly_wait(20)
注意之前我写了个代码看看有什么不同,但是我并不清楚有什么不同 你写的代码
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import *
driver = webdriver.Chrome()
driver.set_window_position(-1200, 0)
driver.maximize_window()
driver.get("http://www.amazon.com/");
# driver.implicitly_wait(20)
WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.ID, 'twotabsearchtextbox')))
search = driver.find_element(By.ID, 'twotabsearchtextbox')
search.click()
search.send_keys('Laptop hp core i5')
第二个问题是我有时看到WebDriverWait方法赋值给一个变量,我的意思是这样的:
element = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.ID, 'twotabsearchtextbox'))))
有什么区别?
我以为 WebDriverWait 只是等待项目出现
和 driver.implicitly_wait(20)
函数
等待页面加载。
至于我是怎么回事,两种方法都在等待页面加载
隐式等待
通过
driver.implicitly_wait(10)
显式等待
element = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.ID, 'twotabsearchtextbox')))
注意事项
不要混用隐式和显式等待。这样做会导致不可预测的等待时间。例如,设置 10 秒的隐式等待和 15[=36 秒的显式等待=] 秒可能导致在 20 秒后发生超时。