簡體   English   中英

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

[英]How do I get current URL in Selenium Webdriver 2 Python?

我試圖在 Selenium 中進行一系列導航后獲取當前 url。 我知道 ruby​​ 有一個名為 getLocation 的命令,但我找不到 Python 的語法。

為 Python 2 使用 current_url 元素:

print browser.current_url

對於 Python 3 及更高版本的 selenium:

print(driver.current_url)

根據這個文檔(一個充滿好東西的地方:)):

driver.current_url

或者,請參閱官方文檔: https : //www.selenium.dev/documentation/en/webdriver/browser_manipulation/#get-current-url

Selenium2Library 有 get_location():

import Selenium2Library
s = Selenium2Library.Selenium2Library()
url = s.get_location()

另一種方法是檢查 chrome 中的 url 欄以找到元素的 id,讓您的 WebDriver 單擊該元素,然后使用 selenium 中的鍵常用功能發送用於復制和粘貼的鍵,然后將其打印出來或將其存儲為變量等。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM