1

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)

2 Answers2

0
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)
BhavinT
  • 338
  • 1
  • 10
0

Your code for Chrome is not necessarily exclusive to Chrome; it works for Brave as well. Below is your code, practically unchanged, showing the URL of the active tab in a Brave window (i.e. "brave.com"). The results are shown in the terminal at the bottom of the Visual Studio Code window. Python 3.8.10 (64-bit) was used.

enter image description here

Opera will require a different control identifier.

Side note, if you're curious which identifiers are available, you can call print_control_identifiers() to get a detailed breakdown. Here is a portion of the results for an Opera instance:

Control Identifiers:

Pane - 'Secure, Fast...'    (L123, T229, R868, B663)
['Secure, Fast...', 'Pane', 'Secure, Fast...', 'Pane0', 'Pane1']
child_window(title="Secure, Fast...", control_type="Pane")
   | 
   | Document - 'Browser DOWNLOADS'    (L171, T310, R860, B655)
   | ['Browser DOWNLOADS', 'Document', 'Document0', 'Document1']
   | child_window(title="Browser DOWNLOADS", auto_id="21440384", control_type="Document")
   | 
   | Pane - ''    (L131, T229, R860, B655)
   | ['Pane3', 'Browse privately.', 'Browse privately.Pane0', 'Browse privately.Pane1']
   | 
   | TitleBar - ''    (L0, T0, R0, B0)
   | ['TitleBar']
   | 
   | Pane - ''    (L131, T229, R860, B655)
   | ['Pane4', 'Browse privately. Search privately. And ditch Big Tech.Pane2']
   |    | 
   |    | Pane - 'Browser non-client'    (L131, T229, R860, B655)
   |    | ['Browser non-clientPane', 'Pane5', 'Browser non-client']
   |    | child_window(title="Browser non-client", control_type="Pane")
   |    |    | 
   |    |    | Pane - ''    (L131, T230, R860, B655)
   |    |    | ['Pane8', 'Browse privately. Search privately. And ditch Big Tech.Pane3']
   |    |    |    | 
   |    |    |    | Pane - ''    (L171, T230, R860, B655)
   |    |    |    | ['Pane9', 'Browse privately. Search privately. And ditch Big Tech.Pane4']
   |    |    |    |    | 
   |    |    |    |    | Toolbar - 'Navigation'    (L171, T271, R860, B310)
   |    |    |    |    | ['Navigation', 'Toolbar', 'NavigationToolbar']
   |    |    |    |    | child_window(title="Navigation", control_type="ToolBar")
   |    |    |    |    |    | 
   |    |    |    |    |    | Pane - ''    (L173, T276, R858, B305)
   |    |    |    |    |    | ['Pane10']
   |    |    |    |    |    |    | 
   |    |    |    |    |    |    | Edit - 'Address bar'    (L310, T276, R787, B305)
   |    |    |    |    |    |    | ['Edit', 'Edit0', 'Edit1']
   |    |    |    |    |    |    | child_window(title="Address bar", control_type="Edit")
   |    |    |    |    |    |    |    | 
   |    |    |    |    |    |    |    | Edit - 'Address field'    (L395, T276, R546, B305)
   |    |    |    |    |    |    |    | ['Edit2']
   |    |    |    |    |    |    |    | child_window(title="Address field", control_type="Edit")

Note at the bottom of the (truncated) output we find the following:

child_window(title="Address field", control_type="Edit")

Using this title, instead of "Address and search bar" for Chrome/Brave, will successfully return the expected data (note that the protocol is also returned with the address in Opera, whereas it is excluded in Brave/Chrome):

enter image description here

I hope this is helpful!

Sampson
  • 265,109
  • 74
  • 539
  • 565