get the current opened page url in opera in python

To get the current opened page URL in Opera using Python, you can use the following code:

main.py
import win32gui

def get_browser_url():
    browser = "Opera"
    handle = win32gui.GetForegroundWindow()
    if win32gui.GetClassName(handle) == "Chrome_WidgetWin_1" and win32gui.GetWindowText(handle).endswith(f" - {browser}"):
        return win32gui.GetWindowText(handle).split(" - ")[-2]
292 chars
8 lines

This code uses win32gui module to get the handle of the active window and then checks if it's an Opera browser window by using its class name and title.

If it's an Opera browser window, the function extracts the URL from the window title and returns it.

Note: This code works in Windows environment only. If you're using a different operating system, you need to find a way to get the active window handle and extract the URL from the window title according to your OS/browser.

related categories

gistlibby LogSnag