如何在 Selenium 中使用 Python 獲取頁面的標題和 URL?


我們可以在 Selenium 中獲取頁面的標題和 URL。

若要獲取瀏覽器的標題,則需要使用 title 方法。

若要獲取頁面的 URL,則需要使用 current_url 方法。

如果我們需要測試是否導航到了正確的頁面和頁面標題,便可使用這兩種方法。

例項

程式碼實現

from selenium import webdriver
#browser exposes an executable file
#Through Selenium test we will invoke the executable file which will then #invoke actual browser
driver = webdriver.Chrome(executable_path="C:\chromedriver.exe")
# to maximize the browser window
driver.maximize_window()
#get method to launch the URL
driver.get("https://tutorialspoint.tw/index.htm")
# to print the page title in console
print(driver.title)
# to print the current URL in console
print(driver.current_url)
#to refresh the browser
driver.refresh()
#to close the browser
driver.close()

更新日期: 2020 年 7 月 29 日

1 千 + 次瀏覽

開啟職業生涯

透過完成課程獲取認證

立即開始
廣告