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.
Asked
Active
Viewed 2.9e+01k times
227
-
Selenium doc explains it all :https://www.selenium.dev/docs/site/en/webdriver/browser_manipulation/#get-current-url – anandharshan Jun 29 '20 at 04:05
4 Answers
408
Use current_url element for Python 2:
print browser.current_url
For Python 3 and later versions of selenium:
print(driver.current_url)
-
167
-
3suppose, there is a chain of page p1->p2->p3 where p2 on the basis of credentials in p1 is redirecting to either p1 or p3. How to get the url just after clicking which I am not able to understand. selenium doesnot stop at that page and is directly giving me the url of p3. how to go for this? – proprius Jan 07 '16 at 09:54
-
@proprius https://stackoverflow.com/questions/35592602/how-can-i-get-a-intermediate-url-from-a-redirect-chain-from-selenium-using-pytho – ed22 Jul 09 '19 at 09:59
-
more importantly, where is this documented on https://selenium-python.readthedocs.io/ ? – Robert Johnstone Mar 17 '20 at 09:33
-
1@RobertJohnstone: https://selenium-python.readthedocs.io/api.html?highlight=current_url#selenium.webdriver.remote.webdriver.WebDriver.current_url – xibalba1 Sep 14 '21 at 20:05
91
According to this documentation (a place full of goodies:)):
driver.current_url
or, see official documentation: https://www.selenium.dev/documentation/en/webdriver/browser_manipulation/#get-current-url
pbaranski
- 20,172
- 17
- 93
- 109
7
Selenium2Library has get_location():
import Selenium2Library
s = Selenium2Library.Selenium2Library()
url = s.get_location()
Milanka
- 1,552
- 17
- 15
2
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.
LiamººT
- 59
- 2