Forced to wait
The easiest method for setting wait is forced to wait, in fact, time.sleep () method, no matter what the circumstances, let the program run for a certain pause time, after time continues to run; the disadvantage is not intelligent, the set time is too short, the elements have not yet load out, it will still be an error; time is too long, it will be a waste of time, do not underestimate a few seconds each time, case more, a large amount of code, a number of seconds it will affect the overall speed up; so use this as little as possible
Invisible wait
driver.implicitly_wait (), set up a hidden waiting time, whether the page has finished loading in a period of time, if completed, would be the next step; no load completed within the set time, it will report a timeout load;
# -*- coding: utf-8 -*-
from selenium import webdriver
import time
driver = webdriver.Chrome()
driver.implicitly_wait(20) # 隐性等待,最长等30秒
driver.get('https://www.baidu.com')
time.sleep(3)
driver.quit()
The disadvantage is not smart, because as an element widely used ajax technology, page load times often are local, that is, when the entire page does not load completely, it may be an element that we need to have loaded, and then it is necessary and then wait for the entire page to load, execute the next step, and can not wait to meet this hidden;
Another point, the waiting time is set recessive global, after the beginning of the set off, the entire process will be effective the program is running, will wait for page loads; do not need to set a time again;
Dominant wait
WebDriverWait (driver, 20, 0.5) .until (expected_conditions.presence_of_element_located (locator)), selenium in the wait module WebDriverWait () method, with or until until_not method, then assisted with some determination conditions, can constitute such a scenario: each of the number of seconds to view a locator element is visible, if visible stop waiting, if not visible continue to wait until exceeds the allotted time, reported a timeout exception; of course, can determine not visible within an element is in the specified time, etc. and other various scenes of it, you need to choose your own judgment based on the actual condition of the scene;
# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
driver = webdriver.Firefox()
driver.get('https://huilansame.github.io')
WebDriverWait(driver,20,0.5).until(
EC.presence_of_element_located((By.LINK_TEXT, 'CSDN')))
print driver.find_element_by_link_text('CSDN').get_attribute('href')
driver.close()
Providing a number of conditions may provide a determination module expected_conditions:
selenium.webdriver.support.expected_conditions(模块)
这两个条件类验证title,验证传入的参数title是否等于或包含于driver.title
title_is
title_contains
这两个人条件验证元素是否出现,传入的参数都是元组类型的locator,如(By.ID, 'kw')
顾名思义,一个只要一个符合条件的元素加载出来就通过;另一个必须所有符合条件的元素都加载出来才行
presence_of_element_located
presence_of_all_elements_located
这三个条件验证元素是否可见,前两个传入参数是元组类型的locator,第三个传入WebElement
第一个和第三个其实质是一样的
visibility_of_element_located
invisibility_of_element_located
visibility_of
这两个人条件判断某段文本是否出现在某元素中,一个判断元素的text,一个判断元素的value
text_to_be_present_in_element
text_to_be_present_in_element_value
这个条件判断frame是否可切入,可传入locator元组或者直接传入定位方式:id、name、index或WebElement
frame_to_be_available_and_switch_to_it
这个条件判断是否有alert出现
alert_is_present
这个条件判断元素是否可点击,传入locator
element_to_be_clickable
这四个条件判断元素是否被选中,第一个条件传入WebElement对象,第二个传入locator元组
第三个传入WebElement对象以及状态,相等返回True,否则返回False
第四个传入locator以及状态,相等返回True,否则返回False
element_to_be_selected
element_located_to_be_selected
element_selection_state_to_be
element_located_selection_state_to_be
最后一个条件判断一个元素是否仍在DOM中,传入WebElement对象,可以判断页面是否刷新了
staleness_of