当前位置:网站首页>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

边栏推荐
- C2338 Cannot format an argument. To make type T formattable provide a formatter<T> specialization:
- 多个全局异常处理类,怎么规定执行顺序
- DNS forward query:
- 修改MySQL密码
- Understand software testing
- The pressure of large institutions in the bear market has doubled. Will the giant whales such as gray scale, tether and micro strategy become 'giant thunder'?
- Practice of enterprise ab/testing platform
- JMeter test result output
- Pytest attempts to execute the test case without skipping, but the case shows that it is all skipped
- 熊市里的大机构压力倍增,灰度、Tether、微策略等巨鲸会不会成为'巨雷'?
猜你喜欢

Pytest -- write and manage test cases

Win 10 find the port and close the port

Inno Setup 制作安装包

These two mosquito repellent ingredients are harmful to babies. Families with babies should pay attention to choosing mosquito repellent products

Arctic code vault contributor

Software testing assignment - the next day

100000 bonus is divided up. Come and meet the "sister who braves the wind and waves" among the winners

2022-06-23 VGMP-OSPF-域間安全策略-NAT策略(更新中)

Software testing learning - day 3

Pits encountered in the use of El checkbox group
随机推荐
Mise en place d'un environnement de développement de fonctions personnalisées
Code management tools
Setting up the development environment of dataworks custom function
Software testing learning - day 3
DBNet:具有可微分二值化的实时场景文本检测
How can I split a string at the first occurrence of “-” (minus sign) into two $vars with PHP?
Condition annotation in uni-app realizes cross segment compatibility, navigation jump and parameter transfer, component creation and use, and life cycle function
centos php7.2.24升级到php7.3
服务器如何设置多界面和装IIS呢?甜甜给你解答!
Jenkins
[vscode - vehicle plug-in reports an error] cannot find module 'xxx' or its corresponding type declarations Vetur(2307)
Reading notes of "learn to ask questions"
Laravel frame step pit (I)
Golang operation redis: write and read hash type data
CentOS switches and installs mysql5.7 and mysql8.0
PHP install composer
crontab定时任务
In depth analysis of reentrantlock fair lock and unfair lock source code implementation
Modify MySQL password
Understand software testing