Python-WEB automation - three kinds of standby mode

When there is less than when the positioning elements, such as drop-down box when not, other pop-up location;
generally two kinds of problem: 1, with a frame; 2, no waiting plus


The following three kinds of learning standby mode:

1. forced to wait for sleep (xx)
This method is simple and crude, regardless of whether the browser has been loaded, the program must wait xx prescribed time to time and we have to continue to implement the following code.
Always waiting is not recommended in this way, it will seriously affect the execution speed of the program.

1
2
3
4
5
6
7
8
# -*- coding:utf-8 -*-
from  selenium  import  webdriver
from  time  import  sleep
driver  =  webdriver.Chrome()
driver.get( 'https://www.baidu.com/' )
sleep( 3 ) #强制性等待3s再执行以下代码
print (driver.current_url)
driver.quit() #退出驱动,关闭所有窗口

  

2. recessive wait implicitly_wait (xx)
of this approach is to set a maximum waiting time, if the page has finished loading all the elements within the specified time, then the next step, otherwise it has been waiting for the deadline before the next step. Mandatory waiting for intelligent than some
! Hidden waiting period for all work throughout the driver, so just set once

1
2
3
4
5
6
7
# -*- coding:utf-8 -*-
from  selenium  import  webdriver
driver  =  webdriver.Chrome()
driver.get( 'https://www.baidu.com/' )
driver.implicitly_wait( 30 ) #隐性等待,最长30s
print (driver.current_url)
driver.quit()

  

3. dominant waiting WebDriverWait
WebDriverWait fit until the class () and until_not () method, based on flexible conditions waiting for
the program to see one every xx seconds, if the condition is true, then the next step, otherwise continue to wait until it exceeds the set the longest time, and then throw TimeoutException.

A. Before use, the first reference to the relevant library

B. determine the positioning of the elements of expression

C. Use expected_conditions corresponding determination condition is generated

WebDriverWait(driver,10,1).until(EC.visibility_of_element_located((By.ID,ele_locator)))
WebDriverWait(driver,10,1).until(EC.visibility_of_element_located((By.XPATH,ele_locator)))

 

D. WebDriverWait call waiting setting total length class, the polling period

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# -*- coding:utf-8 -*-
#A. 使用前,先引用相关库
from  selenium  import  webdriver
from  selenium.webdriver.support.wait  import  WebDriverWait
from  selenium.webdriver.support  import  expected_conditions as EC
from  selenium.webdriver.common.by  import  By
 
driver  =  webdriver.Chrome() #打开Chrome浏览器
driver.get( 'https://www.baidu.com/' ) #打开百度
driver.find_element_by_xpath( '//div[@id="u1"]//a[@name="tj_login"]' ).click() #点击【登录】;click() 方法,可模拟在按钮上的一次鼠标单击。
# B. 确定元素的定位表达式
ele_locator  =  "TANGRAM__PSP_10__footerULoginBtn" #通过id,确定‘用户名登录’元素
 
# C.  使用expected_conditions对应的方法来生成判断条件
# EC.方法名(定位方式,定位表达式)
# EC.visibility_of_element_located(By.ID,ele_locator)#元素可见
 
# D.  调用WebDriverWait类设置等待总时长、轮询周期
# WebDriverWait(driver, 超时时长, 调用频率(默认0.5s)).until(可执行方法, 超时时返回的信息)
# 等待10秒钟,每隔1秒去查看对应的元素是否可见;如果可见,继续下一步操作;如果不可见,则继续等待,直到10s结束,如果元素还是不可见,则抛出超时异常
WebDriverWait(driver, 10 , 1 ).until(EC.visibility_of_element_located((By. ID ,ele_locator)))
1
2
3
driver.find_element_by_id( 'TANGRAM__PSP_10__footerULoginBtn' ).click() #点击【用户名登录】
 
driver.close() #关闭当前窗口

Guess you like

Origin www.cnblogs.com/6J2B2/p/12145660.html