selenium-确认进入了预期页面

selenium确认进入了预期页面

在自动化操作中,浏览器每次进入一个新的需要,都需要确认该页面是否打开或打开的页面是否是预期的页面

需要进行确认页面后方可进行下一步操作

确认页面有很多中方法,像每个页面都有一个固定属性(ng-page = ‘xxx’)来确认,所以确认页面的时候就比较方便,只要找该属性存在即可

例如注册页面:ng-page = ‘register’

  则确认页面时通过定位 ng-page = ‘register’ 则可达到目的

其他定位页面方法:id、url、title等,凡是可以确认确实是预期页面的都可以

下面以百度首页 title 为例

复制代码
# coding = utf-8

from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
import time

driver = webdriver.Chrome()
driver.get('https://www.baidu.com/')
time.sleep(2)
EC.title_contains(“百度搜索”)
# EC.title_is(“百度搜索”)
复制代码

title_contains 与 title_is 区别

  title_contains:包含,即 title 中包含即算 true,否则 false

  title_is: 相等,只有页面的 title 与预期的完全一致才算 true

原文地址:https://www.cnblogs.com/hyf20131113/p/10698232.html