当前位置:网站首页>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
边栏推荐
- Mise en place d'un environnement de développement de fonctions personnalisées
- Practical plug-ins in idea
- 10000小时定律不会让你成为编程大师,但至少是个好的起点
- Software testing assignment - the next day
- Unit test framework + Test Suite
- Winter vacation work of software engineering practice
- How to plan well?
- 爬虫代码基础教学
- Pytest -- write and manage test cases
- 2022-06-23 vgmp OSPF inter domain security policy NAT policy (under update)
猜你喜欢
利用C#实现Pdf转图片
2022年华东师范大学计科考研复试机试题-详细题解
Machine learning | simple but feature standardization methods that can improve the effect of the model (comparison and analysis of robustscaler, minmaxscaler, standardscaler)
10000小時定律不會讓你成為編程大師,但至少是個好的起點
HMS core helps baby bus show high-quality children's digital content to global developers
Software testing learning - day 3
Practice of enterprise ab/testing platform
Setting up the development environment of dataworks custom function
Create your own deep learning environment with CONDA
The dynamic analysis and calculation of expressions are really delicious for flee
随机推荐
Pytest -- write and manage test cases
Redis command
Unittest attempt
[Fiddler actual operation] how to use Fiddler to capture packets on Apple Mobile Phones
dataworks自定义函数开发环境搭建
2022-06-23 VGMP-OSPF-域间安全策略-NAT策略(更新中)
Notes on the core knowledge of Domain Driven Design DDD
Summary of remote connection of MySQL
修改MySQL密码
Heap sort and priority queue
Liang Ning: 30 lectures on brain map notes for growth thinking
服务器如何设置多界面和装IIS呢?甜甜给你解答!
IC_EDA_ALL虚拟机(丰富版):questasim、vivado、vcs、verdi、dc、pt、spyglass、icc2、synplify、INCISIVE、IC617、MMSIM、工艺库
RestHighLevelClient获取某个索引的mapping
Abstract learning
2022-06-23 VGMP-OSPF-域間安全策略-NAT策略(更新中)
2021 year end summary
如何迁移或复制VMware虚拟机系统
Daily question brushing record (11)
Yolov3 learning notes