Objective
The method of getting the page title can be obtained directly with Driver.title, and then the obtained result can be used as an assertion.
This article introduces another way to determine whether the page title and expected results of one, using the previous article Selenium2+python Automation 42-judgment Element (Expected_conditions)
Referred to in the Expected_conditions module in the title_is and title_contains two ways
First, source code analysis
1. First look at the source code, as follows
Class Title_is (object):
"" A expectation for checking the title of a page.
Title is the expected title, which must was an exact match
Returns True if the title matches, False otherwise. ""
"Translation: Check the page title and expectations are exactly the same, if fully consistent, return ture, otherwise return flase" "
def __init__ (self, title):
Self.title = Title
def __call__ (self, driver):
return Self.title = = Driver.title
2. Comment Translation: Check the page title and expectations are exactly the same, if fully consistent, return true, otherwise return flase
3.title_is () This is a class type with two methods in it
4.__INIT__ is the initialization content, parameter is title, required
5.__call__ is to turn an instance into an object, the parameter is driver, the return is self.title = = Driver.title, the Boolean value
Second, Judge Title:title_is ()
1. First import the Expected_conditions module
2. Because this module name is relatively long, so for subsequent calls convenient, renamed to EC (a bit like the database inside the multi-table query when renamed)
3. Open the blog home page after judging the title, return the result is true or false
Third, judge the title contains: Title_contains
1. This class is similar to the one above, except that this is a partial match (similar to the contains syntax inside XPath)
2. Judging title contains ' Shanghai-yo ' string
Iv. Reference Code
# Coding:utf-8
From selenium import Webdriver
From Selenium.webdriver.support import expected_conditions as EC
Driver = Webdriver. Firefox ()
Driver.get ("Http://www.cnblogs.com/yoyoketang")
# Judging title is exactly equal to
title = ec.title_is (U ' Shanghai-leisurely-blog Park ')
Print title (driver)
# Judge Title contains
Title1 = ec.title_contains (U ' Shanghai-yo ')
Print Title1 (Driver)
# Another way of writing
R1 = ec.title_is (U ' Shanghai-leisurely-blog Park ') (driver)
r2 = ec.title_contains (U ' Shanghai-yo ') (driver)
Print R1
Print R2
Selenium2+python Automation 43-Judging title (title_is)