python三种等待方式_Python selenium 三种等待方式解读

已下部分属于摘抄内容:

1. 强制等待

第一种也是最简单粗暴的一种办法就是强制等待sleep(xx),导入time模块,使用time.sleep()

这种叫强制等待,不管浏览器是否加载完了,程序都得等待3秒,3秒一到,继续执行下面的代码,作为调试很有用,有时候也可以在代码里这样等待,不过不建议总用这种等待方式,太死板,严重影响程序执行速度。

2. 隐性等待

第二种办法叫隐性等待,implicitly_wait(xx),

看代码:

1

2

3

4

5

6

7

8

9

# -*- coding: utf-8 -*-

from seleniumimport webdriver

driver= webdriver.Firefox()

driver.implicitly_wait(30)# 隐性等待,最长等30秒

print driver.current_url

driver.quit()

隐形等待是设置了一个最长等待时间,如果在规定时间内网页加载完成,则执行下一步,否则一直等到时间截止,然后执行下一步。注意这里有一个弊端,那就是程序会一直等待整个页面加载完成,也就是一般情况下你看到浏览器标签栏那个小圈不再转,才会执行下一步,但有时候页面想要的元素早就在加载完成了,但是因为个别js之类的东西特别慢,我仍得等到页面全部完成才能执行下一步,我想等我要的元素出来之后就下一步怎么办?有办法,这就要看selenium提供的另一种等待方式——显性等待wait了。

需要特别说明的是:隐性等待对整个driver的周期都起作用,相当于全局变量,所以只要设置一次即可,…

3. 显性等待

第三种办法就是显性等待,WebDriverWait,配合该类的until()和until_not()方法,就能够根据判断条件而进行灵活地等待了。它主要的意思就是:程序每隔xx秒看一眼,如果条件成立了,则执行下一步,否则继续等待,直到超过设置的最长时间,然后抛出TimeoutException。

上面我们说了隐式等待的一个弊端,如果我想等我要的元素一加载出来就执行下一步,该怎么办?这里就要用到显示等待

显示等待要用到WebDriverWait

fromselenium.webdriver.support.wait importWebDriverWait

我们先看一下WebDriverWait的帮助文档:

>>>help(WebDriverWait)Helpon classWebDriverWaitinmoduleselenium.webdriver.support.wait:classWebDriverWait(builtins.object)|Methodsdefinedhere:||__init__(self,driver,timeout,poll_frequency=0.5,ignored_exceptions=None)|Constructor,takes a WebDriverinstance andtimeout inseconds.||:Args:|-driver -Instanceof WebDriver(Ie,Firefox,ChromeorRemote)|-timeout -Numberof seconds before timing out|-poll_frequency -sleep interval between calls

|Bydefault,it is0.5second.|-ignored_exceptions -iterable structure of exception classes ignored

during calls.|Bydefault,it contains NoSuchElementExceptiononly.||Example:|fromselenium.webdriver.support.ui importWebDriverWait||element =WebDriverWait(driver,10).until(lambdax:x.find_element_by_i

d("someId"))||is_disappeared =WebDriverWait(driver,30,1,(ElementNotVisibleException)).\

||until_not(lambdax:x.find_element_by_id("someId").is_displ

ayed())

再看个代码示例:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

# -*- coding: utf-8 -*-

from seleniumimport webdriver

from selenium.webdriver.support.waitimport WebDriverWait

from selenium.webdriver.supportimport expected_conditions as EC

from selenium.webdriver.common.byimport By

driver= webdriver.Firefox()

driver.implicitly_wait(10)# 隐性等待和显性等待可以同时用,但要注意:等待的最长时间取两者之中的大者

locator= (By.LINK_TEXT,'CSDN')

try:

WebDriverWait(driver,20,0.5).until(EC.presence_of_element_located(locator))

print driver.find_element_by_link_text('CSDN').get_attribute('href')

finally:

driver.close()

上例中,我们设置了隐性等待和显性等待,在其他操作中,隐性等待起决定性作用,在WebDriverWait..中显性等待起主要作用,但要注意的是:最长的等待时间取决于两者之间的大者,此例中为20,如果隐性等待时间 > 显性等待时间,则该句代码的最长等待时间等于隐性等待时间。

我们主要用到了WebDriverWait类与expected_conditions模块,下面博主带大家细看一下这两个模块:

WebDriverWait

wait模块的WebDriverWait类是显性等待类,先看下它有哪些参数与方法:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

selenium.webdriver.support.wait.WebDriverWait(类)

__init__

driver: 传入WebDriver实例,即我们上例中的driver

timeout: 超时时间,等待的最长时间(同时要考虑隐性等待时间)

poll_frequency: 调用until或until_not中的方法的间隔时间,默认是0.5秒

ignored_exceptions: 忽略的异常,如果在调用until或until_not的过程中抛出这个元组中的异常,

则不中断代码,继续等待,如果抛出的是这个元组外的异常,则中断代码,抛出异常。默认只有NoSuchElementException。

until

method: 在等待期间,每隔一段时间调用这个传入的方法,直到返回值不是False

message: 如果超时,抛出TimeoutException,将message传入异常

until_not 与until相反,until是当某元素出现或什么条件成立则继续执行,

until_not是当某元素消失或什么条件不成立则继续执行,参数也相同,不再赘述。

method

message

看了以上内容基本上很清楚了,调用方法如下:

WebDriverWait(driver, 超时时长, 调用频率, 忽略异常).until(可执行方法, 超时时返回的信息)

这里需要特别注意的是until或until_not中的可执行方法method参数,很多人传入了WebElement对象,如下:

WebDriverWait(driver, 10).until(driver.find_element_by_id('kw')) # 错误

这是错误的用法,这里的参数一定要是可以调用的,即这个对象一定有 __call__() 方法,否则会抛出异常:

TypeError: 'xxx' object is not callable

在这里,你可以用selenium提供的 expected_conditions 模块中的各种条件,也可以用WebElement的 is_displayed() 、is_enabled()、is_selected() 方法、isEnable用于存储input、select等元素的可编辑状态,可以编辑返回true,否则返回false、isDisplayed()这个有点陌生,本身这个函数用于判断某个元素是否存在页面上(这里的存在不是肉眼看到的存在,而是html代码的存在。某些情况元素的visibility为hidden或者display属性为none,我们在页面看不到但是实际是存在页面的一些元素)、isSelected(),很显然,这个是判断某个元素是否被选中。或者用自己封装的方法都可以,那么接下来我们看一下selenium提供的条件有哪些:

expected_conditions

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

expected_conditions是selenium的一个模块,其中包含一系列可用于判断的条件:

selenium.webdriver.support.expected_conditions(模块)

这两个条件类验证title,验证传入的参数title是否等于或包含于driver.title

title_is

title_contains

这两个条件验证元素是否出现,传入的参数都是元组类型的locator,如(By.ID,'kw')

顾名思义,一个只要一个符合条件的元素加载出来就通过;另一个必须所有符合条件的元素都加载出来才行

presence_of_element_located

presence_of_all_elements_located

这三个条件验证元素是否可见,前两个传入参数是元组类型的locator,第三个传入WebElement

第一个和第三个其实质是一样的

visibility_of_element_located

invisibility_of_element_located

visibility_of

这两个条件判断某段文本是否出现在某元素中,一个判断元素的text,一个判断元素的value

text_to_be_present_in_element

text_to_be_present_in_element_value

这个条件判断frame是否可切入,可传入locator元组或者直接传入定位方式:id、name、index或WebElement

frame_to_be_available_and_switch_to_it

这个条件判断是否有alert出现

alert_is_present

这个条件判断元素是否可点击,传入locator

element_to_be_clickable

这四个条件判断元素是否被选中,第一个条件传入WebElement对象,第二个传入locator元组

第三个传入WebElement对象以及状态,相等返回True,否则返回False

第四个传入locator以及状态,相等返回True,否则返回False

element_to_be_selected

element_located_to_be_selected

element_selection_state_to_be

element_located_selection_state_to_be

最后一个条件判断一个元素是否仍在DOM中,传入WebElement对象,可以判断页面是否刷新了

staleness_of

上面是所有17个condition,与until、until_not组合能够实现很多判断,如果能自己灵活封装,将会大大提高脚本的稳定性。

代码示例:分别为显示等待调用EC模块方法与lambda匿名函数方法

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

# 显示等待

class DriverWait:

def __init__(self):

self.driver = webdriver.Chrome()

self.driver.maximize_window()

# from selenium.webdriver.support import expected_conditions as EC

# 导入expected_conditions模块,使用其中的方法来判断页面元素

# from selenium.webdriver.common.by import By # 使用其中的BY方法wait1方法需要导入这两个模块

def wait1(self):

self.driver.get('http://woniuxy.com/train/teacher.html')

wait1 = WebDriverWait(self.driver, 30, 0.5) # 实例化一个wait对象,30s为timeout,0.5为方法的调用频率

iframe1 = wait1.until(EC.presence_of_element_located((By.ID, 'iframe_company_mini')))

# 需要注意located传入参数为元组

self.driver.switch_to.frame(iframe1)

iframe2 = wait1.until(EC.presence_of_element_located((By.XPATH, "//iframe[@class='ke-edit-iframe']"))) # 调用EC模块中的方法判断

self.driver.switch_to.frame(iframe2)

self.driver.find_element_by_xpath('/html/body').click()

self.driver.find_element_by_xpath('/html/body').send_keys('test')

# 使用lambda 匿名函数,不需要调用EC中的方法

def wait2(self):

self.driver.get('http://woniuxy.com/train/teacher.html')

wait2 = WebDriverWait(self.driver, 30, 0.5)

iframe1 = wait2.until(lambda x: x.find_element_by_id('iframe_company_mini'))

self.driver.switch_to.frame(iframe1)

iframe2 = wait2.until(lambda x: x.find_element_by_xpath("//iframe[@class='ke-edit-iframe']"))

self.driver.switch_to.frame(iframe2)

self.driver.find_element_by_xpath('/html/body').click()

self.driver.find_element_by_xpath('/html/body').send_keys('test')

if __name__ == '__main__':

c = DriverWait()

# c.wait1()

c.wait2()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值