pywinauto:从 MS Edge Canary 地址栏获取 URL
pywinauto: get URL from MS Edge Canary Address Bar
我使用 pywinauto 从 Chrome 浏览器的地址栏中获取 URL 地址没有任何问题。我“应该”能够以相同的方式从 Microsoft Edge Canary 浏览器中获取 URL。然而,似乎有一些东西阻止了 pywinauto 获取 URL;导致它超时并产生后续异常。
代码:
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()
错误:
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'}
我可以通过更改以下行来实现它:
url = dlg.child_window(title="Address and search bar", control_type="Edit").get_value()
收件人:
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()
但是,这是一种糟糕的方法。此外,它在某些情况下不起作用。我非常感谢具有良好 pywinauto 经验的人可以帮助我以正确的方式做到这一点。
PS:我使用 inspect.exe 解决这个问题时运气不佳。据我所知,Edge Canary 和 Google Chrome 都使用元素名称 Address and search bar
。但是,它不适用于 Edge Canary 浏览器。
无论站点是否安全,此代码似乎都有效:
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())
即使这个修补程序有效,直接进入'Address and search bar'元素会更好。如果这不起作用,您应该在 https://github.com/pywinauto/pywinauto/issues
中报告此问题
我使用 pywinauto 从 Chrome 浏览器的地址栏中获取 URL 地址没有任何问题。我“应该”能够以相同的方式从 Microsoft Edge Canary 浏览器中获取 URL。然而,似乎有一些东西阻止了 pywinauto 获取 URL;导致它超时并产生后续异常。
代码:
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()
错误:
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'}
我可以通过更改以下行来实现它:
url = dlg.child_window(title="Address and search bar", control_type="Edit").get_value()
收件人:
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()
但是,这是一种糟糕的方法。此外,它在某些情况下不起作用。我非常感谢具有良好 pywinauto 经验的人可以帮助我以正确的方式做到这一点。
PS:我使用 inspect.exe 解决这个问题时运气不佳。据我所知,Edge Canary 和 Google Chrome 都使用元素名称 Address and search bar
。但是,它不适用于 Edge Canary 浏览器。
无论站点是否安全,此代码似乎都有效:
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())
即使这个修补程序有效,直接进入'Address and search bar'元素会更好。如果这不起作用,您应该在 https://github.com/pywinauto/pywinauto/issues
中报告此问题