使用 pywinauto 获取 Opera、Firefox 和 Brave 浏览器当前打开标签页的 URL。

huangapple go评论80阅读模式
英文:

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.
这是我迄今为止的进展。

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)
英文:

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)

答案1

得分: 0

from pywinauto import Desktop, Application
browser_title = "Opera"  # 根据浏览器更改为 "Firefox" 或 "Brave"
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)
英文:
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)

答案2

得分: 0

你的Chrome代码不一定专门用于Chrome;它也适用于Brave。以下是几乎没有更改的代码,显示了Brave窗口中活动标签的URL(即"brave.com")。结果显示在Visual Studio Code窗口底部的终端中。使用了Python 3.8.10(64位)。

Opera将需要不同的控件标识符

另外,如果你想知道有哪些可用的标识符,可以调用print_control_identifiers()来获得详细的信息。以下是Opera实例的一部分结果:

控件标识符:

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")

在(截断的)输出的底部,我们找到了以下内容:

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

使用这个标题,而不是Chrome/Brave中的"Address and search bar",将成功返回期望的数据(请注意,Opera中的地址也包括协议,而在Brave/Chrome中则不包括)。

希望这对你有帮助!

英文:

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.

使用 pywinauto 获取 Opera、Firefox 和 Brave 浏览器当前打开标签页的 URL。

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):

使用 pywinauto 获取 Opera、Firefox 和 Brave 浏览器当前打开标签页的 URL。

I hope this is helpful!

huangapple
  • 本文由 发表于 2023年5月6日 21:25:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76189136.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定