如何在 Selenium Webdriver 2 Python 中獲取當前 URL?


我們可以透過 Selenium 驅動程式獲取頁面的當前 URL。其方法為 current_url,此方法獲取當前頁面 URL,隨後我們可以在控制檯中列印結果。

語法

s = driver.current_url

讓我們找出當前導航到的頁面 URL,我們便可獲得 https://tutorialspoint.tw/index.htm 作為輸出。

 

示例

from selenium import webdriver
driver = webdriver.Chrome(executable_path="C:\chromedriver.exe")
driver.implicitly_wait(0.5)
driver.get("https://tutorialspoint.tw/index.htm")
#identify current URL with current_url
l= driver.current_url
print(Current URL is: " + l)
driver.close()
 

輸出

 

 


更新於: 18-Sep-2020

897 人瀏覽

職業起航

完成課程後獲得認證

開始
廣告
© . All rights reserved.