2024-06-16 17:16:19 发布
网友
我尝试使用Pywinauto来获得Chrome选项卡的url,如下所示:
Pywinauto
url
(伪代码)
我已经完成了第一步,但仍停留在第二步。同样,我不知道如何处理第三步。因此,我认为我的方法是不正确的和不有效的。 有谁能建议我该怎么做或者给我一个更好的方法吗?在
只是把所有的评论总结在一个答案里。。。在
如果没有pywinauto 0.5.4和剪贴板抓取之类的技巧,pywinauto 0.5.4就无法从Chrome获取URL。即将到来的pywinauto 0.6.0将能够正确地做到这一点。在
pywinauto 0.5.4
pywinauto 0.6.0
如果Chrome已经在运行,只需使用connect而不是start。在
connect
start
我还建议使用TypeKeys("^c", set_foreground=False),因为URL已经在{F6}之后集中,并且焦点可能会切换到主窗口。在
TypeKeys("^c", set_foreground=False)
{F6}
Application().connect(title=u'How can I get a url from Chrome by Python? - Stack Overflow - Google Chrome', found_index=0)也更好,因为标题应该是唯一的。如果有两个窗口,found_index=0或{}是有用的。在
Application().connect(title=u'How can I get a url from Chrome by Python? - Stack Overflow - Google Chrome', found_index=0)
found_index=0
要获取剪贴板数据:
from pywinauto import clipboard print(clipboard.GetData())
如果您只想将剪贴板中的内容粘贴到字符串中,您可以使用其中一个包^{}或{a2},这两个包都是pip安装的。在
import pyperclip print(pyperclip.paste()) #or equivalently... import clipboard print(clipboard.paste())
如果我复制此页面的url,将给出输出'http://stackoverflow.com/questions/35475103/how-can-i-get-a-url-from-chrome-by-python'(两次…)。在
'http://stackoverflow.com/questions/35475103/how-can-i-get-a-url-from-chrome-by-python'
只是把所有的评论总结在一个答案里。。。在
如果没有
pywinauto 0.5.4
和剪贴板抓取之类的技巧,pywinauto 0.5.4
就无法从Chrome获取URL。即将到来的pywinauto 0.6.0
将能够正确地做到这一点。在如果Chrome已经在运行,只需使用
connect
而不是start
。在我还建议使用
TypeKeys("^c", set_foreground=False)
,因为URL已经在{F6}
之后集中,并且焦点可能会切换到主窗口。在Application().connect(title=u'How can I get a url from Chrome by Python? - Stack Overflow - Google Chrome', found_index=0)
也更好,因为标题应该是唯一的。如果有两个窗口,found_index=0
或{要获取剪贴板数据:
如果您只想将剪贴板中的内容粘贴到字符串中,您可以使用其中一个包^{} 或{a2},这两个包都是pip安装的。在
如果我复制此页面的url,将给出输出
'http://stackoverflow.com/questions/35475103/how-can-i-get-a-url-from-chrome-by-python'
(两次…)。在相关问题 更多 >
编程相关推荐