> 文档中心 > selenium--显示等待(中)--详解篇

selenium--显示等待(中)--详解篇


前言

这里是清安,上一章讲了显示等待的理论以及部分用法,本章我们讲一讲Expected_conditions通常也叫EC模块。

注意:不论3.0的还是4.0的,都有这个模块。本章所用的是4.2的。

本章主要以封装的形式进行讲解。上车了!

倒包

from selenium.webdriver.support import expected_conditions as EC

title_is

检查页面标题的期望。title是预期的标题,必须完全匹配如果标题匹配,则返回True,否则返回false。

# ----清安—---
# 微信:qing_an_an
# 公众号:测个der

from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver

class Brouser:

    fox = webdriver.Firefox()
    wait = WebDriverWait(fox, 5)

    def get_url(self,url):
        self.fox.get(url)

    def title(self,value):
        self.wait.until(EC.title_is(value),message='标题不匹配哦')


if __name__ == '__main__':
    b = Brouser()
    b.get_url('http://shop.aircheng.com/simple/login')
    b.title('用户登录 - iWebShop商城演示')

「对以上代码做一个解析,后续都是沿用如上代码」

此处的写法可能跟你们在网上看到的不一样,不过问题不大,怎么写看自己来。主要是方法的书写这里直接将驱动、显示等待写成了类中的属性,定义了一个url方法,用于打开浏览器地址的。

title_is判断浏览器标题的,如果标题不对,则会输出message的提示信息,例如:

selenium.common.exceptions.TimeoutException: Message: 标题不匹配哦

最后又一个执行入口,在里面实例化了类以及调用类中的各种方法。需要传值就传值,不需要的直接调用

title_contains

检查标题是否包含区分大小写的子字符串。title是所需的标题片段当标题匹配时返回True,否则返回False

# ----清安—---
# 微信:qing_an_an
# 公众号:测个der

from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver

class Brouser:

    fox = webdriver.Firefox()
    wait = WebDriverWait(fox, 5)

    def get_url(self,url):
        self.fox.get(url)

    def contains_title(self,value):
        self.wait.until(EC.title_contains(value))


if __name__ == '__main__':
    b = Brouser()
    b.get_url('http://shop.aircheng.com/simple/login')
    b.contains_title('用户登录 - iWebShop商城演示')
    b.contains_title('iWebShop商城演示')

这一项,用的不多,也可以用来判断是否包含指定、特定的文本,如上所示。

presence_of_element_located

检查DOM上是否存在元素的期望一页的。这并不一定意味着元素是可见的。定位器-用于查找元素找到WebElement后返回该WebElement

# ----清安—---
# 微信:qing_an_an
# 公众号:测个der

from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium import webdriver

class Brouser:

    fox = webdriver.Firefox()
    wait = WebDriverWait(fox, 5)

    def get_url(self,url):
        self.fox.get(url)

    def presence_located(self,value,*ele):
        el = self.wait.until(EC.presence_of_element_located(ele),message='没有发现期望的元素')
        el.send_keys(value)


if __name__ == '__main__':
    b = Brouser()
    b.get_url('http://shop.aircheng.com/simple/login')
    b.presence_located('qingan',By.NAME,'login_info')

「上述所说定位器-用于查找元素找到WebElement后返回该WebElement,所以我们在presence_of_element_located(ele)传入这个元素之后,并经过显示等待判断知道了元素存在这个界面上,那就返回这个元素,我们就可以直接拿着这个值进行下一步操作,例如上述的输入操作。」

此外,上述也说了这并不一定意味着元素是可见的,意味着不可见元素也能检测出来,但是你无法做其他操作。

visibility_of_element_located

检查元素是否存在于页面和可见。可见性意味着不仅显示元素但其高度和宽度也大于0。定位器-用于查找元素找到并可见WebElement后返回该WebElement

# ----清安—---
# 微信:qing_an_an
# 公众号:测个der

from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium import webdriver

class Brouser:

    fox = webdriver.Firefox()
    wait = WebDriverWait(fox, 5)

    def get_url(self,url):
        self.fox.get(url)

    def visibility_located(self,value,*ele):
        el = self.wait.until(EC.visibility_of_all_elements_located(ele), message='没有发现期望的元素')
        el[0].send_keys(value)


if __name__ == '__main__':
    b = Brouser()
    b.get_url('http://shop.aircheng.com/simple/login')
    b.visibility_located('qingan',By.NAME,'password')

此处值得说的是el[0],为什么是el[0],因为此处这样的写法返回后是个list列表,所以需要通过取值的方式将元素取出来。

url_to_be

检查当前url的期望值。url是预期的url,必须完全匹配。如果url匹配,则返回True,否则返回false

# ----清安—---
# 微信:qing_an_an
# 公众号:测个der

from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium import webdriver

class Brouser:

    fox = webdriver.Firefox()
    wait = WebDriverWait(fox, 5)

    def get_url(self,url):
        self.fox.get(url)

    def url_be(self,url):
        self.wait.until(EC.url_to_be(url))


if __name__ == '__main__':
    b = Brouser()
    b.get_url('http://shop.aircheng.com/simple/login')
    b.url_be('http://shop.aircheng.com/simple/login')

判断url还有另外的几种方式,都大同小异,可以了解一下。

「url_matches」 : 检查当前url的期望值。pattern是预期的模式,必须是完全匹配的如果url匹配,则返回True,否则返回false。 「url_contains」 : 检查当前url是否包含区分大小写的子字符串。url是所需url的片段,url匹配时返回True,否则返回False
「url_changes」 : 检查当前url的期望值。url是预期的url,不能完全匹配如果url不同,则返回True,否则返回false。

visibility_of

检查已知存在于页面的DOM是可见的。可见性意味着元素不仅仅是显示,但高度和宽度也大于0。元素是WebElement在WebElement可见时返回(相同的)WebElement

# ----清安—---
# 微信:qing_an_an
# 公众号:测个der

from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium import webdriver

class Brouser:

    fox = webdriver.Firefox()
    wait = WebDriverWait(fox, 5)

    def get_url(self,url):
        self.fox.get(url)

    def visibility_(self,*ele):
        el = self.wait.until(EC.visibility_of(self.fox.find_element(*ele)))
        el.click()


if __name__ == '__main__':
    b = Brouser()
    b.get_url('http://shop.aircheng.com/simple/login')
    b.visibility_(By.NAME, 'remember')

这里值得注意的是,visibility_of_element_located跟visibility_of很类似。与上面的写法不同EC.visibility_of里面我写的是定位,而非单纯的元素。这也是一个区别点。在后续的使用中注意一下。

最后!说一下,上文中明明提起了True跟Flase但是没有看文中提起。
一、可以给定一个变量例如上文中的el,直接print(el)即可看到
二、是因为为True直接可以向下操作其他步骤了,没必要非要看到True。如果是False就会告诉你错误了。