pythonfirefoxoperapywinautobrave

getting the url of the current open tab of opera, firefox and Brave browser using pywinauto


I am working on a project that requires the URL of a tab open in a browser window. Till now I have done the code for Chrome and Edge but am unable for Opera, Firefox, and Brave browser. Any amount of help would be appreciated.
This is my progress till far.

from win32gui import GetForegroundWindow
from win32process import GetWindowThreadProcessId
from pywinauto.application import Application
import time

time.sleep(3)
window = GetForegroundWindow()
tid, pid = GetWindowThreadProcessId(window)

# chrome

app = Application(backend="uia").connect(process=pid, time_out=10)
dlg = app.top_window()
url = dlg.child_window(title="Address and search bar", control_type="Edit").get_value()
print(url)

# edge 

app = Application(backend='uia').connect(process=pid, found_index=0)
dlg = app.top_window()
wrapper = dlg.child_window(title="App bar", control_type="ToolBar")
url = wrapper.descendants(control_type='Edit')[0].get_value()
print(url)

Solution

  • from pywinauto import Desktop, Application
    browser_title = "Opera"  # Change this to "Firefox" or "Brave" depending on the browser
    def get_browser_url(browser_title):
        app = Application(backend="uia").connect(title=browser_title)
        window = app.top_window()
        address_bar = window.child_window(title="Address and search bar", control_type="Edit")
        url = address_bar.get_value()
        return url
    
    current_url = get_browser_url(browser_title)
    print(current_url)