In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article is to share with you about the way Selenium waits. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
1. Forced wait (sleep) from time import sleepsleep (3) # forced wait for 3 seconds
Disadvantages: because the speed of Web loading depends on the hardware tested, network speed, server response time and other factors. If the waiting time is too long, it is easy to waste time, and if the waiting time is too short, it may cause an error when the web has not finished loading the element that needs to be located.
As the wait time is uncertain, using too much sleep will affect the running speed and greatly reduce efficiency, so it is recommended to use forced wait as little as possible in testing.
2. Implicit waiting (implicitly_wait) # implicit waiting for 10sdriver.implicitly_wait (10)
Introduction: implicit waiting is global for all elements, set the waiting time such as 10 seconds, if it occurs within 10 seconds, then continue to go down, otherwise throw an exception. It can be understood as constantly refreshing within 10 seconds to see if the element is loaded.
Usage scenario: implicit waiting only needs to be declared once, usually after opening the browser. The declaration is valid for the entire life cycle of the drvier, and there is no need to repeat the declaration later. There is a problem with implicit waiting, that is, the program will always wait for the entire page to load, that is, in general, you will see that the small circle in the browser tab bar no longer turns before performing the next step, but sometimes the desired elements of the page have already been loaded, but because things such as individual js are particularly slow, you still have to wait until the page is complete to perform the next step.
3. Show wait (expected_conditions)
Summary: show that waiting is for an element alone. Set a wait time such as 5 seconds to check whether it appears every 0.5 seconds. If it occurs at any time before 5 seconds, then continue to go down. Generally, you need to work with the until () and until_not () methods of this class until the maximum set time is exceeded, and then throw a timeout error TimeoutException. Here are some of the most commonly used methods:
1. Determine whether element is visible: visibility_of_element_located (locator) (visible represents element is not hidden, and element width and height are not equal to 0)
From selenium.webdriver.support import expected_conditions as ECfrom selenium.webdriver.support.wait import WebDriverWaitfrom selenium.webdriver.common.by import By# sample target = EC.visibility_of_element_located (By.ID,'user') # with until () (wait for the element to be visible) # 5 indicates the maximum timeout, which is measured in seconds by default 1 indicates the interval step of detection. During the waiting period, the method in until or until_not is called at regular intervals (the default is 0.5 seconds). Until it returns True or False.WebDriverWait (driver, 5,1) .cake (EC.visibility_of_element_located (By.ID,'user')) # with until_not () use (wait element is not visible) WebDriverWait (driver, 5,1). Until_not (EC.visibility_of_element_located (By.ID,'user')) # is encapsulated as a function def wait_eleLocated (self, loc, timeout=30, poll_frequency=0.5) in the class Model=None): ": param loc: element localized expression Tuple type, expression (element location type, element location method) Example: (By.ID, "kw"): param timeout: timeout: param poll_frequency: polling frequency: param model: when waiting for failure, screenshot operation. The function to be expressed in the image file: return:None "" self.logger.info (f 'waiting for "{model}" element). Positioning method: {loc}') try: start = datetime.now () WebDriverWait (self.driver, timeout) Poll_frequency) .waiting (EC.visibility_of_element_located (loc)) end = datetime.now () self.logger.info (f' waiting for "{model}" length: {end-start}') except TimeoutException: self.logger.exception (F' waiting for "{model}" element failed Positioning method: {loc}') # screenshot self.save_webImgs (f "waiting for an exception in element [{model}]") raise
two。 Determine whether an element is loaded into the dom tree: presence_of_element_located (locator) (does not mean that the element must be visible)
From selenium.webdriver.support import expected_conditions as ECfrom selenium.webdriver.common.by import By target = EC.presence_of_element_located (By.ID,'user')
3. Determine whether an element is visible and clickable: element_to_be_clickable (locator)
From selenium.webdriver.support import expected_conditions as ECfrom selenium.webdriver.common.by import By target = EC.element_to_be_clickable (By.ID,'user')
4. Determine whether an element is selected: element_to_be_selected (element) (commonly used in drop-down lists)
From selenium.webdriver.support import expected_conditions as ECfrom selenium.webdriver.common.by import By element = driver.find_element_by_class_name ('selector') EC.element_to_be_selected (element) Thank you for your reading! This is the end of this article on "what are the ways of waiting for Selenium?". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.