当前位置:网站首页>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_conditionsA large number of available conditional methods are provided in ( May refer toexpected_conditionsSource 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

边栏推荐
- UTC time, GMT time, CST time
- 每日刷题记录 (十一)
- error C2017: 非法的转义序列
- Laravel Web Framework
- Sorting out the core ideas of the pyramid principle
- 2022 cisp-pte (III) command execution
- Operation principle of lua on C: Foundation
- These two mosquito repellent ingredients are harmful to babies. Families with babies should pay attention to choosing mosquito repellent products
- [day15] introduce the features, advantages and disadvantages of promise, and how to implement it internally. Implement promise by hand
- Condition annotation in uni-app realizes cross segment compatibility, navigation jump and parameter transfer, component creation and use, and life cycle function
猜你喜欢

golang操作redis:写入、读取kv数据

Ruoyi interface permission verification

每日刷题记录 (十一)

Sorting out the core ideas of the pyramid principle

2022-06-23 vgmp OSPF inter domain security policy NAT policy (under update)

10000小时定律不会让你成为编程大师,但至少是个好的起点

利用C#实现Pdf转图片

Personally design a highly concurrent seckill system

EasyExcel

Notes on the core knowledge of Domain Driven Design DDD
随机推荐
JMeter JSON extractor extracts two parameters at the same time
HMS core helps baby bus show high-quality children's digital content to global developers
多个全局异常处理类,怎么规定执行顺序
Software testing assignment - day 1
error C2017: 非法的转义序列
JMeter test result output
How does the insurance company check hypertension?
Notes on the core knowledge of Domain Driven Design DDD
SQL implementation merges multiple rows of records into one row
Create your own deep learning environment with CONDA
UTC time, GMT time, CST time
Unit test notes
MySQL installation
The difference between CONDA and pip
vmware虚拟机C盘扩容
爬虫代码基础教学
Laravel frame step pit (I)
2021 year end summary
mysql误删root账户导致无法登录
php安装composer