- 页面等待
- -- coding:utf-8 --from selenium import webdriverfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ECfrom selenium.webdriver.support.ui import Selectfrom time import sleepchromedriver = "./chromedriver"driver = webdriver.Chrome(executable_path=chromedriver)driver.get(" http://www.meituan.com/index/changecity/initiative")try : #判断form表单ajax加载完成标记:id属性 element = WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.XPATH, '//select[contains(@id,"yui_3_16")]')) ) s1 = Select(driver.find_element_by_name('province')) # 实例化Select s1.select_by_visible_text("山西") sleep(5) # seconds s2 = Select(driver.find_element_by_name('city')) # 实例化Select s2.select_by_visible_text("大同") sleep(5) # seconds driver.find_element_by_xpath('//*[@value="确定"]').submit() #是个坑,容性问题 #driver.find_element_by_class_name('btn btn-mini').submit()finally: print 'end' #driver.quit()
页面等待
是非常重要的一部分,现在的网页越来越多采用了 Ajax 技术,这样程序便不能确定何时某个元素完全加载出来了。这会让元素定位困难而且会提高产生 ElementNotVisibleException
的概率。
所以 Selenium 提供了两种等待方式,一种是隐式等待,一种是显式等待。
隐式等待是等待特定的时间,显式等待是指定某一条件直到这个条件成立时继续执行。
显式等待
指定某个条件,然后设置最长等待时间。如果超出这个时间还没有找到元素,那么便会抛出异常了。
-- coding:utf-8 --from selenium import webdriverfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ECfrom selenium.webdriver.support.ui import Selectfrom time import sleepchromedriver = "./chromedriver"driver = webdriver.Chrome(executable_path=chromedriver)driver.get("http://www.meituan.com/index/changecity/initiative")try: #判断form表单ajax加载完成标记:id属性 element = WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.XPATH, '//select[contains(@id,"yui_3_16")]')) ) s1 = Select(driver.find_element_by_name('province')) # 实例化Select s1.select_by_visible_text("山西") sleep(5) # seconds s2 = Select(driver.find_element_by_name('city')) # 实例化Select s2.select_by_visible_text("大同") sleep(5) # seconds driver.find_element_by_xpath('//*[@value="确定"]').submit() #是个坑,容性问题 #driver.find_element_by_class_name('btn btn-mini').submit()finally: print 'end' #driver.quit()
程序默认会 500ms 调用一次来查看元素是否已经生成,如果本来元素就是存在的,那么会立即返回。
下面是一些内置的等待条件,你可以直接调用这些条件,而不用自己写某些等待条件了。
- title_is
- title_contains
- presence_of_element_located
- visibility_of_element_located
- visibility_of
- presence_of_all_elements_located
- text_to_be_present_in_element
- text_to_be_present_in_element_value
- frame_to_be_available_and_switch_to_it
- invisibility_of_element_located
- element_to_be_clickable - it is Displayed and Enabled.
- staleness_of
- element_to_be_selected
- element_located_to_be_selected
- element_selection_state_to_be
- element_located_selection_state_to_be
- alert_is_present
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 10)
element = wait.until(EC.element_to_be_clickable((By.ID,'someid')))
隐式等待
隐式等待目的是让WebDriver在查找某个或某类元素时候容留一定的时间来进行检查。在这个时间内,如果找到就返回。否则就等到超过设置的时间并告知没有找到。当然如果不设置,默认等待时间为0。
当我们设定了这个时间后,在我们下一次设定前,这个时间一直就是隐式等待的时间。代码示例如下:
from selenium import webdriver
chromedriver = "./chromedriver"
driver = webdriver.Chrome(executable_path=chromedriver)
driver.implicitly_wait(30)
driver.get("https://www.google.co.in/")
driver.find_element_by_id(“lst-ib”)
In the above example the implicit waits value is given as 30sec i.e, if web driver able to find the element with in the given span 30sec it immediately returns true else it raises an exception after 30 seconds.
当前内容版权归 piaosanlang 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 piaosanlang .