Get current URL from browser using python

Get current URL from browser using python

If to use Selenium for web navigation:

from selenium import webdriver
driver = webdriver.Firefox()
print (driver.current_url)

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.

Get current URL from browser using python

I just solved a class problem similar to this.
Weve 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 Im 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 dependencies
from splinter import browser
import requests


## go to original page 
browser.visit(url)

## Loop through the page associated with each headline
for headline in titles:
    print(headline.text)
    browser.click_link_by_partial_text(headline.text)
## Now that Im 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)

Leave a Reply

Your email address will not be published. Required fields are marked *