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 > Servers >
Share
Shulou(Shulou.com)06/02 Report--
In UI automated testing, it is inevitable to encounter unstable environment, slow network and so on. When you think there is nothing wrong with positioning, but the program directly reports that the element is invisible, you need to think about whether the element is invisible because the program runs too fast or the page loads too slowly. You have to wait until the element is visible and the program continues to run. In Selenium, the three common waiting methods have their own advantages or disadvantages, so you can try to choose the best waiting mode for different situations after understanding it.
1. Forced wait (sleep)
The easiest way to set waiting is to force waiting, which is actually the time.sleep () method. No matter what the situation is, let the program stop running for a certain period of time and continue to run after that. The disadvantage is that it is not smart, the setting time is too short, and the element has not yet been loaded, then it will still report an error. Setting time is too long, it will be a waste of time, do not underestimate the time of a few seconds at a time, more case, a large amount of code, many a few seconds will affect the overall running speed; so try to use this less.
#-*-coding:utf-8-*-
Import time
From datetime import datetime
Print (datetime.now ()) # get the current time
Time.sleep (10) # set waiting time for 10s
Print (datetime.now ()) # get the current time again
The code is simple, mainly to set a wait time after getting the current time, and then set a thing after the wait time to see the current time after the end of the wait time. From the execution result, we can see that the next step will be executed again only after the execution is finished and wait.
two。 Hidden waiting (implicitly_wait ())
Implicit waiting actually sets a maximum wait time, and if the page loads within the specified time, perform the next step, otherwise wait until the time is over, and then perform the next step. There will be a hole in this implicit wait. We all know that js is usually placed at the end of our body to load. In fact, all the elements on the page have been loaded, but we are still waiting for the whole page to finish loading.
#-*-coding: utf-8-*-
From selenium import webdriver
Import time
Driver = webdriver.Chrome ()
Driver.implicitly_wait (20) # recessive wait, up to 30 seconds
Driver.get ('https://www.baidu.com')
Time.sleep (3)
Driver.quit ()
Implicit waiting works for the entire driver cycle, just set it once at the beginning. Don't use it as a fixed wait, wait implicitly wherever you go.
3. Show wait method (WebDriverWait)
If it is clear that the element to wait for is not found within the specified time, then throw an Exception. The code example is as follows:
#-*-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 ()
WebDriverWait (driver,20,0.5) .notify (expected_conditions.presence_of_element_located (locator)), the WebDriverWait () method of the wait module in selenium, together with the until or until_not method, and some judgment conditions, can form such a scenario: every number of seconds to check whether the element of locator is visible, stop waiting if it is visible, and continue to wait until the specified time is exceeded and a timeout exception is reported. Of course, you can also judge whether an element is invisible within a specified time, and so on. You need to choose the judgment conditions according to your own actual scene.
4. Summary
Implicit wait until driver is fully loaded (such as js, css, etc.); show wait only to verify the existence of elements that need to be loaded; forced wait literally is easy to understand, so don't say much. You can make your own choice according to your needs. Next issue notice: selenium multi-window switching method.
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.