14, three kinds of elements and determining the waiting mode

 

introduction:

  When you feel your position is no problem, but directly reported to the element is not visible, then you can consider is not because the program is running too fast or too slow page loads caused by the element is not visible, it must be added wait, wait element is visible again and continue the program;

 

text:

  1. forced to wait (SLEEP)

  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

 

  2. recessive wait (implicitly_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;

Copy the code
# -*- coding: utf-8 -*-  
from selenium import webdriver
import time

driver = webdriver.Chrome()  
driver.implicitly_wait ( 20 is ) # recessive waiting longest 30 seconds,  
driver.get('https://www.baidu.com')
time.sleep(3)
driver.quit()  
Copy the code

  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;

 

  3. dominant waiting (WebDriverWait)

  WebDriverWait (Driver,  20 is,  0.5) .until (expected_conditions.presence_of_element_located (Locator)), the wait module Selenium 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;

Copy the code
# -*- 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() 
Copy the code
Providing a number of conditions may provide a determination module expected_conditions:
Copy the code
selenium.webdriver.support.expected_conditions(模块)  

These two conditions based verification title, title validate incoming parameter is equal to or contained in driver.title  

title_is  

title_contains  

The two individual elements to verify whether the condition occurs, the parameters are passed Locator tuple type, such as (By.ID, ' kW ' )  

As the name suggests, a just a qualifying element by loading it out; another must all eligible elements are loaded out of the job  

presence_of_element_located  

presence_of_all_elements_located  

These three conditions is visible authentication elements, the first two arguments passed Locator tuple type, the third pass WebElement  

The first and third and its essence is the same  

visibility_of_element_located  

invisibility_of_element_located  

visibility_of  

The two men certain conditions to determine whether the text in an element, the element of text appears a judge, a judge element of value  

text_to_be_present_in_element  

text_to_be_present_in_element_value  

This condition is determined whether the frame can be cut, can be passed directly into the locator element or group targeting: id, name, index or WebElement  

frame_to_be_available_and_switch_to_it  

This condition appears to determine whether there alert  

alert_is_present  

This condition can determine whether the element is clicked, the incoming locator  

element_to_be_clickable  

This element determines whether or not four conditions is selected, the first condition WebElement incoming objects, the second locator incoming tuple  

The third incoming WebElement objects and status, equal returns True, otherwise False  

The fourth incoming locator and state, equal returns True, otherwise False  

element_to_be_selected  

element_located_to_be_selected  

element_selection_state_to_be  

element_located_selection_state_to_be  

The last condition to determine whether an element is still in the DOM, incoming WebElement object, it can determine whether to refresh the page  

staleness_of  

Guess you like

Origin www.cnblogs.com/yhms/p/11797682.html