此篇博客學習的api如標題,分別是:
current_url 獲取當前頁面的url;
page_source 獲取當前頁面的源碼;
title 獲取當前頁面的title;
將以上方法按順序練習一遍,效果如GIF:
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()