Introduction:
When you feel that your positioning is not a problem, but the direct report of the element is not visible, then you can consider whether it is because the program is running too fast or the page load is too slow to cause the element is not visible, it must wait, wait for the element to be visible and then continue to run the program;
Body:
1. Forced Wait (sleep)
The simplest way to set the wait is to force the wait, in fact is the Time.sleep () method, regardless of its situation, let the program pause for a certain period of time, after the time to continue to run, the shortcomings are not intelligent, set the time is too short, the elements have not been loaded, it will also error; Set the time too long, It will waste time, do not underestimate the time of a few seconds, more case, the code is large, many a few seconds will affect the overall speed of operation; so try to use less.
2. Hidden Wait (implicitly_wait ())
Driver.implicitly_wait (), the hidden wait set a time, in a period of time the page is loaded complete, if completed, the next step, in the set time is not loaded completed, will be reported time-out load;
#-*-Coding:utf-8-*-from selenium import webdriverimport timedriver = Webdriver. Chrome () driver.implicitly_wait (20) # Hidden wait, longest wait 30 seconds driver.get (' https://www.baidu.com ') time.sleep (3) Driver.quit ()
The disadvantage is not intelligent, because with the wide application of AJAX technology, the elements of the page can often be loaded in time, that is, when the entire page is not loaded, it is possible that the elements we need are already loaded, then it is necessary to wait for the entire page to load, perform the next step, But the recessive wait cannot satisfy this point;
Another point, the implicit waiting for the setting of the global, at the beginning of the setup, the entire program will be valid during the run, will wait for the page to load complete;
3. Explicit Wait (webdriverwait)
Webdriverwait (Driver, 0.5). Until (expected_conditions.presence_of_element_located (locator)), The Webdriverwait () method of the Wait module in selenium, combined with the until or Until_not method, can be used to make a scene with a few judgment conditions: how many seconds to see if the locator element is visible, If it is visible, stop waiting, if it is not visible, continue to wait until more than the specified time, the report time-out exception, of course, you can determine whether an element is not visible in the specified time and so on the various scenarios, you need to choose according to your own actual scene selection criteria;
#-*-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< C4/>driver = Webdriver. Firefox () driver.get (' Https://huilansame.github.io ') webdriverwait (driver,20,0.5). Until ( ec.presence_of _element_located ((By.link_text, ' CSDN '))
The Expected_conditions module provides a number of conditions that can be judged:
Selenium.webdriver.support.expected_conditions (module) These two conditional classes verify the title, verifying that the passed parameter title is equal to or contained in the Driver.title title_is Title_ Contains these two-person conditions verify that the element is present, the passed-in parameters are locator of the tuple type, such as (by.id, ' kw '), as the name implies, a single element that meets the criteria is loaded and passed; the other must be loaded with all eligible elements. Presence_of_element_located presence_of_all_elements_located These three conditions verify that the element is visible, the first two incoming parameters are locator of the tuple type, and the third incoming webelement The first and third of its essence is the same visibility_of_element_located invisibility_of_element_located visibility_of These two person conditions determine whether a piece of text appears in an element, A judgment element of the text, a judgment element of the value text_to_be_present_in_element text_to_be_present_in_element_value this condition to determine whether the frame can be cut, You can pass in locator tuples or direct incoming targeting methods: ID, name, index, or webelement frame_to_be_available_and_switch_to_it this condition to determine if there are alert occurrences Alert_is_ Present this condition determines whether the element can be clicked, passed in locator element_to_be_clickable these four criteria determine whether the element is selected, the first condition passed in the Webelement object, and the second incoming locator tuple The third incoming Webelement object and the state, equal returns True, otherwise returns false fourth incoming locator and the 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_beThe last condition determines whether an element is still in the DOM and passes in the Webelement object to determine if the page is refreshed staleness_of
Python selenium three ways to wait