当前位置:网站首页>Selenium key knowledge explanation
Selenium key knowledge explanation
2022-07-03 06:58:00 【Wu Daxi】
One 、 Explicit wait and implicit wait :
1、 Just wait :
Mandatory waiting , The thread sleeps for a while
time.sleep(3)
2、 An implicit wait :
Set a waiting time , Polling for ( Default 0.5 second ) Whether the element appears , If the element appears within the waiting time , Then exit and wait , If it does not appear, an exception is thrown
self.driver.implicitly_wait(3)
(1) shortcoming : Set the global wait , Act on all find_element() Method ;
(2) Find the element and exit , Whether or not the element is loaded into clickable state .
3、 Explicit waiting :
Define conditions in code , Continue executing code only when a condition occurs .WedDriverWait
coordination until()
or until_not()
Method , Wait according to the judgment conditions .
def findelement(self, loc, wait=10, num=0.5):
''' Positioning elements '''
element = WebDriverWait(self.driver, wait,num).until(EC.presence_of_element_located(loc))
return element
Program every once in a while ( Default 0.5 second ) Make condition judgment , If the condition holds , Then go to the next step , Otherwise, keep waiting , Until the set maximum waiting time is exceeded .
(1) Be careful :
until()
oruntil_not()
You need to pass a method name in ( Brackets are not required , Method name only ), This method must have an input parameter ( May refer to until Source code )expected_conditions
A large number of available conditional methods are provided in ( May refer toexpected_conditions
Source code )
Two 、web Control positioning and common operations
1、xpath
(1) shortcoming :
- Slow speed : Traverse from beginning to end
(1) advantage :
- It can be used for appnium and selenium
2、css_selector:
- It's using css Selectors , Faster , Use style to locate
- by.Id,by.name,by.tag_name,by.class_name The essence is css_selector
3、Actions
3.1、ActionChains:
perform PC Mouse click on the end , double-click , Right click and drag events :
3.2、TouchActions:
simulation PC And mobile click , slide , Drag and drop , Multi touch and other gesture operations ;
3、 ... and 、 other
1、 Multi window processing :
- Gets the handle to the current page :
driver.current_window_handle
- Get the current handle :
driver.window_handles
- Switch to the handle of the desired operation :
driver.switch_to.window()
def switch_window(self,data,type='index',isClose=False):
''' Switch windows , According to the index,url,title,handle Switch '''
to_window=current_window = self.driver.current_window_handle # Current window handle
windows = self.driver.window_handles # All window handles
if type=='index' and isinstance(data,int): # Switch according to the order of windows
to_window=windows[data]
elif type=='handle':
to_window = data
else:
for handle in windows:
self.driver.switch_to.window(handle)
if type == 'url':
current_url = self.driver.current_url
if data in current_url:
to_window=handle
break
elif type=='title':
current_title=self.driver.title
if data in current_title:
to_window = handle
break
# Switch to the desired handle
self.driver.switch_to.window(to_window)
if isClose:
# Close the first handle , Keep only the second handle
self.driver.switch_to.window(current_window)
self.driver.close()
return current_window,to_window
2、frame Handle :
- Switch to frame:
switch_to.frame(reference)
(reference Is the parameter passed in , Used to locate frame, You can pass in id、name、index as well as selenium Of WebElement object ) - from frame Switch back to the main document :
driver.switch_to.default_content()
- nesting frame The operation of :
driver.switch_to.parent_frame()
( Congzi frame Cut back to the father frame, If it is already the main document , No effect )
3、JS To deal with :
The treatment method is to use js Show the hidden , The method is as follows :
js="document.getElementById('li').style.display='block';"
driver.execute_script(js)
1、 Processing of time control :
4、 Pop up processing mechanism :
5、 Solve the problem of scanning code for login :
1、 Reuse existing browsers :
Will not restart a browser , Reuse browsers that are already running
after , We can run our test code on the launched browser .
2、 By getting the current browser cookie The way
- Take the background management login of enterprise wechat as an example , Use the new browser , Open the login scanning page , Mandatory waiting 10 Seconds to complete the code scanning manually , Scan the code to enter the management background ;
- After logging in , Used in code driver Return to... On the current page cookies value , And save for json file ;
- Subsequent test cases , You can use the newly saved cookie, Add in driver Then you can
边栏推荐
- Pytorch exercise items
- [open source project recommendation colugomum] this group of undergraduates open source retail industry solutions based on the domestic deep learning framework paddlepadddle
- mongodb
- Error c2017: illegal escape sequence
- 2022-06-23 vgmp OSPF inter domain security policy NAT policy (under update)
- dataworks自定义函数开发环境搭建
- Selenium - by changing the window size, the width, height and length of different models will be different
- UTC时间、GMT时间、CST时间
- Reading notes of "learn to ask questions"
- Operation principle of lua on C: Foundation
猜你喜欢
2022 cisp-pte (III) command execution
2022 East China Normal University postgraduate entrance examination machine test questions - detailed solution
Dbnet: real time scene text detection with differentiable binarization
Yolov1 learning notes
Ruoyi interface permission verification
2022-06-23 VGMP-OSPF-域間安全策略-NAT策略(更新中)
Software testing learning - day 3
Software testing assignment - the next day
Reading notes of "learn to ask questions"
2022年华东师范大学计科考研复试机试题-详细题解
随机推荐
PHP install composer
Modify MySQL password
[untitled] 8 simplified address book
IC_ EDA_ All virtual machine (rich Edition): questasim, vivado, VCs, Verdi, DC, Pt, spyglass, icc2, synthesize, innovative, ic617, mmsim, process library
Unit test framework + Test Suite
[classes and objects] explain classes and objects in simple terms
Selenium - by changing the window size, the width, height and length of different models will be different
Personally design a highly concurrent seckill system
多个全局异常处理类,怎么规定执行顺序
每日刷題記錄 (十一)
vmware虚拟机C盘扩容
[untitled] 5 self use history
(翻译)异步编程:Async/Await在ASP.NET中的介绍
EasyExcel
MySQL installation
How does the insurance company check hypertension?
Mise en place d'un environnement de développement de fonctions personnalisées
[untitled]
Laravel Web Framework
Condition annotation in uni-app realizes cross segment compatibility, navigation jump and parameter transfer, component creation and use, and life cycle function