selenium.獲取當前頁面的url、源碼、title


此篇博客學習的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()

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM