Get Current Url From Browser Using Python
Solution 1:
If to use Selenium for web navigation:
from selenium import webdriver
driver = webdriver.Firefox()
print (driver.current_url)
Solution 2:
You can get the current url by doing
path_info = request.META.get('PATH_INFO')
http_host = request.META.get('HTTP_HOST').
You can add these two to get complete url.
Basically request.META returns you a dictionary which contain a lot of information. You can try it.
Solution 3:
I just solved a class problem similar to this. We've been using Splinter to walk through pages (you will need to download splinter and Selenium). As I walk through pages, I periodically need to pull the url of the page I'm currently on. I do that using the command new_url = browser.url Below is an example of my code.
I do this using the following code.
##import dependenciesfrom splinter import browser
import requests
## go to original page
browser.visit(url)
## Loop through the page associated with each headlinefor headline in titles:
print(headline.text)
browser.click_link_by_partial_text(headline.text)
## Now that I'm on the new page, I need to grab the url
new_url = browser.url
print(new_url)
## Go back to original page
browser.visit(url)
Solution 4:
Below is the solution I use in Django.
For eg.,. if browser url is https://www.example.com/dashboard
try:
from urlparse import urlparse
except ImportError:
from urllib.parse import urlparse
frontend_url = request.META.get('HTTP_REFERER')
url = urlparse(frontend_url)
print (url)
# ParseResult(scheme='https', netloc='example.com', path='/dashboard', params='', query='', fragment='')
Solution 5:
You could use the requests module:
import requests
link = "https://stackoverflow.com"data = requests.request("GET", link)
url = data.url
Post a Comment for "Get Current Url From Browser Using Python"