常見問題:前端
分析緣由:web
一些同窗的解決解決方案:windows
分析解決方案:後端
什麼是顯示等待?網絡
class WebDriverWait(object): def __init__(self, driver, timeout, poll_frequency=POLL_FREQUENCY, ignored_exceptions=None): """構造函數,須要一個WebDriver實例並以秒爲單位超時 :參數: - driver - 傳入webdriver實例對象 - timeout - 超時時間,單位 秒 - poll_frequency - 程序休眠時間,默認0.5秒 - ignored_exceptions - 忽略的異常,若是在調用until或until_not的過程當中拋出這個元組中的異常, 則不中斷代碼,繼續等待,若是拋出的是這個元組外的異常,則中斷代碼,拋出異常。默認只有NoSuchElementException。 使用例子: from selenium.webdriver.support.ui import WebDriverWait \n element = WebDriverWait(driver, 10).until(lambda x: x.find_element_by_id("someId")) \n is_disappeared = WebDriverWait(driver, 30, 1, (ElementNotVisibleException)).\ \n until_not(lambda x: x.find_element_by_id("someId").is_displayed()) """ def until(self, method, message=''): """在規定的超時時間內,調用傳入的方法,直到返回的結果爲真,不然拋出超時的異常""" screen = None stacktrace = None end_time = time.time() + self._timeout while True: try: value = method(self._driver) if value: return value except self._ignored_exceptions as exc: screen = getattr(exc, 'screen', None) stacktrace = getattr(exc, 'stacktrace', None) time.sleep(self._poll) if time.time() > end_time: break raise TimeoutException(message, screen, stacktrace) def until_not(self, method, message=''): """與unitl方法相反,很少解釋,項目中基本用不到""" end_time = time.time() + self._timeout while True: try: value = method(self._driver) if not value: return value except self._ignored_exceptions: return True time.sleep(self._poll) if time.time() > end_time: break raise TimeoutException(message)
經常使用app
每一個類的具體用法查看源碼,寫的很是清楚
/Python/Python36/Lib/site-packages/selenium/webdriver/support異步
# 判斷頁面title等於預期值 title_is # 判斷頁面title包含預期字符串 title_contains # 判斷當前url等於預期url url_to_be # 判斷當前url包含預期字符串 url_matches # 正則匹配 re url_contains # 包含 in # 判斷當前url不等於預期url url_changes # 判斷元素顯現並定位成功 visibility_of_element_located # 判斷元素顯現(不必定能定位,能定位的元素大小需大於0) visibility_of # 判斷獲取文本信息包含預期的文本信息 text_to_be_present_in_element # 判斷獲取屬性值的信息包含預期的文本信息 text_to_be_present_in_element_value # 判斷iframe能夠切換,若是爲真則切換,反之返回False frame_to_be_available_and_switch_to_it # 判斷元素能夠點擊 element_to_be_clickable # 判斷元素被選中 element_to_be_selected element_located_to_be_selected # 判斷窗口數量 number_of_windows_to_be # 判斷新窗口打開 number_of_windows_to_be # 警告框顯現 alert_is_present
不經常使用函數
presence_of_element_located presence_of_all_elements_located visibility_of_any_elements_located visibility_of_all_elements_located invisibility_of_element_located invisibility_of_element(invisibility_of_element_located) staleness_of element_selection_state_to_be element_located_selection_state_to_be
demo.py測試
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support.wait import WebDriverWait driver = webdriver.Chrome() driver.get('https://www.baidu.com') # 輸入框定位器 locator_of_search = (By.CSS_SELECTOR,'#kw') # 元素顯現並定位成功後,再也不等待,反則拋出超時異常 WebDriverWait(driver=driver,timeout=10,poll_frequency=0.5).until(EC.visibility_of_element_located(locator_of_search)) driver.find_element(*locator_of_search).send_keys('測試顯示等待')