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

边栏推荐
- 卡特兰数(Catalan)的应用场景
- 100000 bonus is divided up. Come and meet the "sister who braves the wind and waves" among the winners
- 10000小時定律不會讓你成為編程大師,但至少是個好的起點
- [C /vb.net] convert PDF to svg/image, svg/image to PDF
- (翻译)异步编程:Async/Await在ASP.NET中的介绍
- JMeter test result output
- mongodb
- Unittest attempt
- 【code】if (list != null && list.size() > 0)优化,集合判空实现方式
- Winter vacation work of software engineering practice
猜你喜欢
![[open source project recommendation colugomum] this group of undergraduates open source retail industry solutions based on the domestic deep learning framework paddlepadddle](/img/f8/0e3fbfd13bf06291a73200552ff17a.png)
[open source project recommendation colugomum] this group of undergraduates open source retail industry solutions based on the domestic deep learning framework paddlepadddle

Pits encountered in the use of El checkbox group

Winter vacation work of software engineering practice

La loi des 10 000 heures ne fait pas de vous un maître de programmation, mais au moins un bon point de départ

2022 East China Normal University postgraduate entrance examination machine test questions - detailed solution

POI excel percentage
![[vscode - vehicle plug-in reports an error] cannot find module 'xxx' or its corresponding type declarations Vetur(2307)](/img/7f/3d6b6ea5319f7165e07baf0a78d318.jpg)
[vscode - vehicle plug-in reports an error] cannot find module 'xxx' or its corresponding type declarations Vetur(2307)

DBNet:具有可微分二值化的实时场景文本检测

Software testing learning - day one

(翻译)异步编程:Async/Await在ASP.NET中的介绍
随机推荐
Setting up the development environment of dataworks custom function
Software testing learning - day 3
Practical plug-ins in idea
centos php7.3安装redis扩展
Software testing assignment - the next day
MySQL installation
The difference between CONDA and pip
Application scenarios of Catalan number
100000 bonus is divided up. Come and meet the "sister who braves the wind and waves" among the winners
A letter to graduating college students
CentOS php7.3 installing redis extensions
[untitled] 8 simplified address book
Notes on the core knowledge of Domain Driven Design DDD
Golang operation redis: write and read hash type data
DBNet:具有可微分二值化的实时场景文本检测
Yolov2 learning and summary
Interface learning
Asynchronous programming: async/await in asp Net
Unit test notes
Inno Setup 制作安装包