程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

[Python automated test 30] three waiting and three switching of Web Automation

編輯:Python

List of articles

  • One 、 Preface
  • Two 、 Three waiting to be explained
    • 2.1 First meet the three waiting ways
    • 2.2 Three waiting instructions
  • 3、 ... and 、 Window switch

One 、 Preface

This article will mainly explain selenium Three waiting modes in , In addition, there is a portal for a series of articles below , It's still being updated , Interested partners can also go to check , Don't talk much , Let's have a look ~

Series articles :
Series articles 1:【Python automated testing 1】 meet Python The beauty of the
Series articles 2:【Python automated testing 2】Python Installation configuration and PyCharm Basic use
Series articles 3:【Python automated testing 3】 First knowledge of data types and basic syntax
Series articles 4:【Python automated testing 4】 Summary of string knowledge
Series articles 5:【Python automated testing 5】 List and tuple knowledge summary
Series articles 6:【Python automated testing 6】 Dictionary and collective knowledge summary
Series articles 7:【Python automated testing 7】 Data operator knowledge collection
Series articles 8:【Python automated testing 8】 Explanation of process control statement
Series articles 9:【Python automated testing 9】 Function knowledge collection
Series articles 10:【Python automated testing 10】 File basic operation
Series articles 11:【Python automated testing 11】 modular 、 Package and path knowledge collection
Series articles 12:【Python automated testing 12】 Knowledge collection of exception handling mechanism
Series articles 13:【Python automated testing 13】 class 、 object 、 Collection of attribute and method knowledge
Series articles 14:【Python automated testing 14】Python Basic and advanced exercises of automatic test
Series articles 15:【Python automated testing 15】unittest The core concept and function of test framework
Series articles 16:【Python automated testing 16】 Test case data separation
Series articles 17:【Python automated testing 17】openpyxl Secondary packaging and data driven
Series articles 18:【Python automated testing 18】 Configuration file analysis and practical application
Series articles 19:【Python automated testing 19】 Log system logging Explain
Series articles 20:【Python automated testing 20】 Construction of interface automation test framework model
Series articles 21:【Python automated testing 21】 Interface automation test practice 1 _ Interface concept 、 Project introduction and test process Q & A
Series articles 22:【Python automated testing 22】 Interface automation test practice II _ Interface framework modification and use case optimization
Series articles 23:【Python automated testing 23】 Interface automation test practice III _ Dynamic parameterization and data forgery
Series articles 24:【Python automated testing 24】 Interface automation test practice IV _Python Operating the database
Series articles 25:【Python automated testing 25】 Interface automation test practice 5 _ Database assertion 、 Interface Association and related management optimization
Series articles 26:【Python automated testing 26】 Interface automation test practice 6 _pytest frame +allure Explain
Series articles 27:【Python automated testing 27】Web Automated testing theory 、 Environment construction and common operations
Series articles 28:【Python automated testing 28】html Basic grammar
Series articles 29:【Python automated testing 29】Xpath、 Axis operation and CSS Detailed explanation of element positioning

Two 、 Three waiting to be explained

2.1 First meet the three waiting ways

stay selenium There are three ways to wait , Namely Mandatory waiting The recessive waiting as well as Explicit wait .

Waiting mode The main function is to prevent elements from being successfully located , Sometimes computers are more Carton 、 Web browser Slow loading , Or occasionally The network fluctuated Will cause a button 、 The loading speed of the picture becomes slow or will not be loaded for a while and a half , But the execution efficiency of the code is very fast , This is likely to result in when the code is executing , The web browser is still loaded , You can't see a button or interface in the page at all , Then the element search will naturally fail , So the three waiting is to solve this problem .

2.2 Three waiting instructions

Mandatory waiting : Mandatory waiting, as the name suggests, is mandatory waiting , If in selenium Forced waiting is set in , It means you have to wait whether you find the element or not X second , The grammar is time.sleep( Number of seconds ), for example time.sleep(3) -> Mandatory wait on behalf of 3 Second , Generally speaking, it will not be added when automated testing or self-developed tools time.sleep(), The main reason is the uncertain time factor of waiting , Short time may fail , A long time may cause a waste of time , Therefore, it is not often used in testing , But forced waiting also has advantages , Can be used when debugging is needed , This can clearly show the execution logic and steps .

The recessive waiting : from webdriver Methods provided , After setting, the implicit wait will take effect globally , It does not target an element , That is, when locating elements , You need to wait until all elements of the page are loaded , Will execute the next statement . If the set time is exceeded, an exception is thrown , The grammar is :driver.implicitly_wait( Number of seconds ) , But implicit waiting has certain limitations , Can only be used when looking for elements , It can only judge whether the element has been successfully loaded , For example, when the registration button of a website is grayed out , Then implicit waiting cannot be operated , When switching windows , You can't use implicit waiting

Explicit wait : Explicit waiting can be used in different scenarios , such as : Wait for whether an element can be clicked 、 Whether an element is visible, etc , This is different from implicit waiting , Explicit waiting and implicit waiting do not conflict , Can exist at the same time . Fixed usage of explicit waiting :

""" The use of explicit waiting : 1、 Set up a wait / Timer , During this time, we will always look for this element and judge whether it meets a certain condition If in the meantime , Meet this condition , Then skip will be executed , If it does not meet the requirements, it will continue to do cyclic operation 2、 Set a condition , When this condition is met, you can jump out of the loop , If you are not satisfied, keep waiting """
wait = WebDriverWait(driver, timeout=10, poll_frequency=0.2)
locator = ("xpath", "'//input[@name="server" and @value="281"]'")
# wait for , Until a condition meets the requirements , Go and find locator Elements , Then judge whether it can be clicked , If you can't click, continue the cycle , If satisfied, click directly to continue 
new_locator = wiat.until(expected_conditions.element_to_be_clickable(locator))
new_locator.click()
# Whether the element can be seen 
ec.visibility_of_element_located(locator)
# Whether the element is loaded 
ec.presence_of_element_located()
# Look at the title 
ec.title_is(" Baidu ")
# One url Whether the corresponding content is included 
ec.url_contains("http://www.baidu.com")
ketang_el.click()

3、 ... and 、 Window switch

As the name suggests, window switching is to switch the browser window , Sometimes we are doing automated testing or making some Web When using tools, you need to switch windows , Otherwise, it is impossible to locate the elements of the window without switching the window , Can't find the corresponding operation , stay selenium There are three main ways of window switching in :

switch The way : switch The way is a fixed usage , The grammar is :driver.switch_to.window(driver.window_handles[ Indexes ]), The fixed window can be found by subscript , So as to realize switching

iframe The way : iframe It is equivalent to nesting a sub web page in a web page , We can't directly locate the content of sub pages , Need to use iframe The way :
1、 If we find one iframe, We need to switch first , The syntax of one of them is :driver.switch_to.frame("name attribute ")
2、 Another way is to find the switch first iframe Elements , Through driver.switch_to.frame(iframe_element) Way to find
3、 If only one iframe Under the circumstances , It's very convenient to index , You can quickly find , The grammar is driver.switch_to.frame(0), It should be noted that , Most of the time , We're not going to do this , It is possible to add... In subsequent iterations of some websites iframe, This may lead to positioning errors , So as to increase and modify the code 、 The cost of maintaining the code .

alert The way : In some original web pages , Still keep alert The way , If you enter a web page and don't close it alert, Then the web content cannot be displayed completely , There is no way to carry out relevant operations , We go through driver.switch_to.alert You can switch in the same way , Be careful alert No parentheses after , And then use alert.accept() Shut down , In this way, the element can be located


All right. ~ The above is all the content shared in this article , Have you learned ? I hope I can help you !



  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved