Python:从Google Chrome获取主动选项卡URL

发布于 2025-01-20 22:17:10 字数 555 浏览 2 评论 0原文

我可以找到旧的答案,但没有一个适用于 python 3.9.10 和 Chrome 100.0.4896.88

这是不起作用的代码之一。今天我们有什么办法可以做到这一点吗? 如果有人可以提供适用于 Windows 10 操作系统的通用信息,欢迎。

from win32gui import GetForegroundWindow
from win32process import GetWindowThreadProcessId

from pywinauto.application import Application



window = GetForegroundWindow()
tid, pid = GetWindowThreadProcessId(window)
app = Application().connect(process=pid)
dlg = app.top_window()
title = "Address and search bar"
url = dlg.child_window(title=title, control_type="Edit").get_value()
print(url)

I could find old answers but none of them working for python 3.9.10 and Chrome 100.0.4896.88

This is one of the code that is not working. Is there any way we can do that today?
If anyone can give generic information that applies to windows 10 os, its welcome.

from win32gui import GetForegroundWindow
from win32process import GetWindowThreadProcessId

from pywinauto.application import Application



window = GetForegroundWindow()
tid, pid = GetWindowThreadProcessId(window)
app = Application().connect(process=pid)
dlg = app.top_window()
title = "Address and search bar"
url = dlg.child_window(title=title, control_type="Edit").get_value()
print(url)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

简美 2025-01-27 22:17:10

只要您在时间结束之前单击镀铬窗口。

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

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

The following code works as long as you click on the Chrome window before the end of the time.sleep(3) :

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

time.sleep(3)
window = GetForegroundWindow()
tid, pid = GetWindowThreadProcessId(window)
app = Application(backend="uia").connect(process=pid, time_out=10)
dlg = app.top_window()
title = "Address and search bar"
url = dlg.child_window(title=title, control_type="Edit").get_value()
print(url)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文