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)

Get current URL in Python

Try this:

self.request.url

Also, if you just need the querystring, this will work:

self.request.query_string

And, lastly, if you know the querystring variable that you're looking for, you can do this:

self.request.get("name-of-querystring-variable")

How to get current url from browser

You can get current path using httprequest

from openerp import http

print http.request.httprequest.full_path

But you should not call it in init method, AFAIK init is going to be called once at startup.

Edit:

If what you need is to pass an info from a model to another, I suggest you passing the value through context.

Say the audit has a many2one relation to action model.

class audit(models.Model):

_name = "audit"

action = fields.Many2one('action', string="Action")

Pass the context on your view :

<field name="action" context="{'from':'This is from audit model'}"/>

and when you're at action model you can call the context using this method:

self._context.get('from', False)

It doesn't have to be a field to pass context, you can do it in button too.

opening a web browser and get url histories in python

There are a few main approaches for automating interactions with browsers:

  1. Telling a program how and what to click, like a human would, sometimes using desktop OS automation tools like Applescript
  2. Parse files that contain browser data (will vary browser to browser, here is Firefox)
  3. Use a tool or library that relies on the WebDriver protocol (e.g. selenium, puppeteer)
  4. Access the local SQLite database of the browser and run queries against it

Sounds like 3 is what you need, assuming you're not against bringing in a new dependency.

How to get current URL in python web page?

If you don't have any libraries to do this for you, you can construct your current URL from the HTTP request that gets sent to your script via the browser.

The headers that interest you are Host and whatever's after the HTTP method (probably GET, in your case). Here are some more explanations (first link that seemed ok, you're free to Google some more :).

This answer shows you how to get the headers in your CGI script:

If you are running as a CGI, you can't read the HTTP header directly,
but the web server put much of that information into environment
variables for you. You can just pick it out of os.environ[].

If you're doing this as an exercise, then it's fine because you'll get to understand what's behind the scenes. If you're building anything reusable, I recommend you use libraries or a framework so you don't reinvent the wheel every time you need something.

How do I get current URL in Selenium Webdriver 2 Python?

Use current_url element for Python 2:

print browser.current_url

For Python 3 and later versions of selenium:

print(driver.current_url)


Related Topics



Leave a reply



Submit