如何在Selenium Webdriver 2 Python中获取当前URL?

问题:如何在Selenium Webdriver 2 Python中获取当前URL?

我试图在Selenium中进行一系列导航后获取当前的URL。我知道红宝石有一个名为getLocation的命令,但是我找不到Python的语法。

I’m trying to get the current url after a series of navigations in Selenium. I know there’s a command called getLocation for ruby, but I can’t find the syntax for Python.


回答 0

使用current_url元素。例:

print browser.current_url

Use current_url element for Python 2:

print browser.current_url

For Python 3 and later versions of selenium:

print(driver.current_url)

回答 1

根据此文档(一个放置好东西的地方:)):

driver.current_url

或者,请参阅官方文档:https : //seleniumhq.github.io/docs/site/en/webdriver/browser_manipulation/#get-current-url

According to this documentation (a place full of goodies:)):

driver.current_url

or, see official documentation: https://seleniumhq.github.io/docs/site/en/webdriver/browser_manipulation/#get-current-url


回答 2

Selenium2Library具有get_location():

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

Selenium2Library has get_location():

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

回答 3

另一种方法是检查chrome中的网址栏,找到该元素的id,让WebDriver单击该元素,然后使用selenium中的通用键功能发送用于复制和粘贴的键,然后打印出来或将其存储为变量等。

Another way to do it would be to inspect the url bar in chrome to find the id of the element, have your WebDriver click that element, and then send the keys you use to copy and paste using the keys common function from selenium, and then printing it out or storing it as a variable, etc.