Search code examples
pythonwindowspywinauto

pywinauto: get URL from MS Edge Canary Address Bar


I don't have any issues grabbing the URL address from a Chrome Browser's address bar using pywinauto. I "should" be able to grab the URL from a Microsoft Edge Canary browser in the same fashion. However, there appears to be something preventing pywinauto from grabbing the URL; causing it to timeout and produce a subsequent exception.

Code:

from pywinauto import Application

app = Application(backend='uia')
app.connect(title_re=".*Microsoft​ Edge.*", found_index=0)

captionsearch = Application().connect(title_re=".*Microsoft​ Edge.*", found_index=0)
caption = captionsearch.windows(title_re=".*Microsoft​ Edge.*", found_index=0)
caption = str(caption)

dlg = app.top_window()
url = dlg.child_window(title="Address and search bar", control_type="Edit").get_value()

Errors:

Traceback (most recent call last):
  File "C:\Users\micha\AppData\Local\Programs\Python\Python37\lib\site-packages\pywinauto\application.py", line 258, in __resolve_control
    criteria)
  File "C:\Users\micha\AppData\Local\Programs\Python\Python37\lib\site-packages\pywinauto\timings.py", line 458, in wait_until_passes    
    raise err
pywinauto.timings.TimeoutError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "d:/OneDrive/Dev/Projects/Python/Share-URL-Edge.py", line 42, in <module>
    url = dlg.child_window(title="Address and search bar", control_type="Edit").get_value()
  File "C:\Users\micha\AppData\Local\Programs\Python\Python37\lib\site-packages\pywinauto\application.py", line 379, in __getattribute__
    ctrls = self.__resolve_control(self.criteria)
  File "C:\Users\micha\AppData\Local\Programs\Python\Python37\lib\site-packages\pywinauto\application.py", line 261, in __resolve_control
    raise e.original_exception
  File "C:\Users\micha\AppData\Local\Programs\Python\Python37\lib\site-packages\pywinauto\timings.py", line 436, in wait_until_passes
    func_val = func(*args, **kwargs)
  File "C:\Users\micha\AppData\Local\Programs\Python\Python37\lib\site-packages\pywinauto\application.py", line 222, in __get_ctrl
    ctrl = self.backend.generic_wrapper_class(findwindows.find_element(**ctrl_criteria))
  File "C:\Users\micha\AppData\Local\Programs\Python\Python37\lib\site-packages\pywinauto\findwindows.py", line 87, in find_element
    raise ElementNotFoundError(kwargs)
pywinauto.findwindows.ElementNotFoundError: {'title': 'Address and search bar', 'control_type': 'Edit', 'top_level_only': False, 'parent': <uia_element_info.UIAElementInfo - 'pywinauto: URL from MS Edge Canary Address Bar - Stack Overflow and 1 more pare page - Personal - Microsoft​ Edge', Chrome_WidgetWin_1, 1510144>, 'backend': 'uia'}

I was able to get this to work by changing the below line:

url = dlg.child_window(title="Address and search bar", control_type="Edit").get_value()

To:

url = dlg.child_window(control_type="Edit", found_index=0).get_value()
if url == "":
    url = dlg.child_window(control_type="Edit", found_index=1).get_value()
if url == "":
    url = dlg.child_window(control_type="Edit", found_index=2).get_value()
if url == "":
    url = dlg.child_window(control_type="Edit", found_index=3).get_value()

However, this is a terrible way to do it. Also, it doesn't work in some cases. I would really appreciate someone with good pywinauto experience who can help me to do this the right way.

PS: I didn't have much luck using inspect.exe to troubleshoot this issue. As far as I can tell, Edge Canary and Google Chrome both use the element name Address and search bar. However, it just doesn't work with Edge Canary browser.


Solution

  • This code seems to work regardless if the site is secure or not:

    app = pywinauto.Application(backend='uia')
    app.connect(title_re=".*Microsoft​ Edge.*", found_index=0)
    dlg = app.top_window()
    wrapper = dlg.child_window(title="App bar", control_type="ToolBar")
    url = wrapper.descendants(control_type='Edit')[0]
    print(url.get_value())
    

    Even if this tinker works, it would be better to go directly to the 'Address and search bar' element. If this doesn't work you should report this issue in https://github.com/pywinauto/pywinauto/issues