问题:如何在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.


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。