In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you what are the waiting methods for the core technology of python automated testing selenium. I hope you will get something after reading this article. Let's discuss it together.
In the process of UI automated testing, there may be some situations, such as unstable test environment, slow network and so on. If we do not do any processing, there will be errors that cannot be located to specific elements, resulting in the automatic testing can not be executed smoothly.
Selenium official website manual: Waits | Selenium
In slenium automated testing, there are three main waiting methods:
1 use the sleep mode of python's own module time
Disadvantages: even if the network conditions are good, still wait for a predetermined fixed time, generally not recommended, script debugging can be used.
Sample script:
From selenium import webdriverfrom time import sleepclass TestWait (object): driver = webdriver.Chrome () driver.get (http://www.baidu.com) def test_sleep (self): self.driver.find_element_by_id ("kw"). Send_keys ("sleep test") # sleep (2) # waiting for fixed time self.driver.implicitly_wait (2) # implicit waiting for self .driver.find _ element_by_id ("su") .click () self.driver.quit () if _ _ name__ = ='_ _ main__': wait=TestWait () wait.test_sleep () 2 implicit wait (implicitly_wait)
The implicit waiting time for the setting is the longest, 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.
Note: implicit waiting works for the entire cycle of driver, which is usually set at the beginning. Don't wait as a fixed wait, set implicit wait everywhere.
Sample script:
From selenium import webdriverfrom time import sleep class TestWait (object): driver = webdriver.Chrome () driver.get (http://www.baidu.com) def test_sleep (self): self.driver.find_element_by_id ("kw"). Send_keys ("sleep test") self.driver.implicitly_wait (2) # implicit waiting for self.driver.find_element_by_id ("su"). Click () self.driver.quit () if _ _ name__ = ='_ _ main__': wait=TestWait () wait.test_sleep () 3 shows waiting (WebDriverWait)
Explicit waiting allows wait conditions to occur, so it is ideal for synchronizing state between browsers and their DOM and WebDriver scripts.
Package needs to be introduced: from selenium.webdriver.support.wait import WebDriverWait
WebDriverWait parameter description:
WebDriverWait (driver, timeout=3) .subscription (some_condition)
Two methods: until and util_not
Scene:
Open the Baidu home page, waiting for the page title to appear: Baidu, you will know, and then execute the input search keywords, click the "Baidu once" button.
Sample script:
From selenium import webdriverfrom selenium.webdriver.support.wait import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ecclass TestWait (object): def setup (self): self.driver = webdriver.Chrome () self.driver.get (http://www.baidu.com) def test_webdreiverwait (self): webdreiverwaits = WebDriverWait (self.driver,2) webdreiverwaits.until (ec.title_is) You will know ") self.driver.find_element_by_id (" kw "). Send_keys (" test_webdreiverwait test ") self.driver.find_element_by_id (" su "). Click () def teardown (self): self.driver.quit () if _ _ name__ = ='_ main__': wait=TestWait () wait.test_webdreiverwait ()
Three wait for complete sample scripts:
From selenium import webdriverfrom time import sleepfrom selenium.webdriver.support.wait import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ecclass TestWait (object): def setup (self): self.driver = webdriver.Chrome () self.driver.get ("http://www.baidu.com") def test_sleep (self): self.driver.find_element_by_id (" kw "). Send_keys (" sleep test ") sleep (2) # waiting for a fixed time self.driver.find_element_by_id ("su"). Click () def test_implicitly (self): self.driver.find_element_by_id ("kw"). Send_keys ("implicitly test") self.driver.implicitly_wait (2) # implicit waiting for self.driver.find_element_by_id ("su") .click () def Test_webdreiverwait (self): webdreiverwaits = WebDriverWait (self.driver 2) webdreiverwaits.until (ec.title_is) You know ") self.driver.find_element_by_id (" kw "). Send_keys (" test_webdreiverwait test ") self.driver.find_element_by_id (" su "). Click () def teardown (self): self.driver.quit () if _ _ name__ = ='_ main__': wait=TestWait () # wait.test_sleep () # wait.test_implicitly () wait.test_webdreiverwait ()
[FAQ]: run the footer empty suite:
From selenium import webdriverfrom time import sleepclass TestWait (object): def _ init__ (self): self.driver = webdriver.Chrome () self.driver.get ('http://www.baidu.com') def test_sleep (self): self.driver.find_element_by_id ("kw"). Send_keys ("sleep test") # sleep (2) # wait for a fixed time Self.driver.implicitly_wait (2) # implicitly waiting for self.driver.find_element_by_id ("su") .click () self.driver.quit () if _ _ name__ = ='_ main__': wait=TestWait () wait.test_sleep ()
[solution]: script modification
From selenium import webdriverfrom time import sleepfrom selenium.webdriver.support import expected_conditions as ECfrom selenium.webdriver.support.wait import WebDriverWait class TestCase (object): def setup (self): self.driver = webdriver.Chrome () self.driver.get ('http://www.baidu.com') # sleep (2) def test_sleep (self): self.driver.find_element_by_id (' kw'). Send_keys ( 'selenium') # sleep (2) # thread blocking blocking wait self.driver.find_element_by_id (' su'). Click () # sleep (3) def test_implicitly (self): self.driver.implicitly_wait (10) self.driver.find_element_by_id ('kw'). Send_keys (' selenium') # sleep (2) # Thread blocking blocking wait self.driver.find_element_by_id ('su'). Click () # sleep (3) def test_wait (self): wait = WebDriverWait (self.driver) 2) wait.until (EC.title_is) You know') self.driver.find_element_by_id ('kw'). Send_keys (' selenium') # sleep (2) # Thread blocking blocking wait self.driver.find_element_by_id ('su'). Click () # sleep (3) def teardown (self): self.driver.quit () if _ _ name__ = =' _ _ main__': Case = TestCase () # case.test_sleep () # case.test_implicitly () case.test_wait () finished reading this article I believe you have a certain understanding of "what is the way to wait for the core technology of python automated testing selenium". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!
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.