此篇博客學習的api如標題,分別是:
current_url 獲取當前頁面的url;
page_source 獲取當前頁面的源碼;
title 獲取當前頁面的title;
將以上方法按順序練習一遍,效果如gif:
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
|
from selenium import webdriver from time import sleep sleep( 2 ) driver = webdriver.chrome() driver.get( "https://www.baidu.com/" ) # 移動瀏覽器觀看展示 driver.set_window_size(width = 500 , height = 500 , windowhandle = "current" ) driver.set_window_position(x = 1000 , y = 100 , windowhandle = 'current' ) sleep( 2 ) # 獲取當前頁面title并斷言 title = driver.title print ( "當前頁面的title是:" , title, "\n" ) assert title = = u "百度一下,你就知道" , "頁面title屬性值錯誤!" sleep( 2 ) # 獲取當前頁面的源碼并斷言 pagesource = driver.page_source try : assert u "百度一下,你就不知道" in pagesource, "頁面源碼中未找到'百度一下,你就知道'關鍵字" except : print ( "源碼這里故意斷言錯誤" , "\n" ) sleep( 2 ) # 獲取當前頁面url并斷言 currentpageurl = driver.current_url print ( "當前頁面的url是:" , currentpageurl) assert currentpageurl = = "https://www.baidu.com/" , "當前網頁網址非預期!" sleep( 2 ) driver.quit() |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://www.cnblogs.com/youngleesin/p/10692675.html