如何在 Selenium Webdriver 2 Python 中获取当前 URL?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15985339/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I get current URL in Selenium Webdriver 2 Python?
提问by user2276896
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.
我试图在 Selenium 中进行一系列导航后获取当前 url。我知道 ruby 有一个名为 getLocation 的命令,但我找不到 Python 的语法。
采纳答案by 4d4c
Use current_url element. Example:
使用 current_url 元素。例子:
print browser.current_url
回答by Milanka
Selenium2Library has get_location():
Selenium2Library 有 get_location():
import Selenium2Library
s = Selenium2Library.Selenium2Library()
url = s.get_location()
回答by pbaranski
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
或者,请参阅官方文档:https: //seleniumhq.github.io/docs/site/en/webdriver/browser_manipulation/#get-current-url
回答by LiamooT
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.
另一种方法是检查 chrome 中的 url 栏以找到元素的 id,让您的 WebDriver 单击该元素,然后使用 selenium 中的键常用功能发送用于复制和粘贴的键,然后将其打印出来或将其存储为变量等。

