【问题标题】:How to get the current URL from Chrome using pywinauto 0.6如何使用 pywinauto 0.6 从 Chrome 获取当前 URL
【发布时间】:2017-09-10 20:29:16
【问题描述】:

在这个问题How can I get a url from Chrome by Python? 中,有人提出您可以在 pywinauto 0.6 中从 python 中获取 url。是怎么做到的?

【问题讨论】:

  • 好问题。感谢您的提醒。但是需要一些时间来编写代码 sn -p 无需键入键和单击鼠标即可使用。你有没有尝试过你身边的东西?已经学会了Getting Started Guide
  • 不,抱歉。我使用了复制和剪贴板。
  • 这能回答你的问题吗? Get Chrome tab URL in Python

标签: google-chrome url pywinauto


【解决方案1】:

使用inspect.exe(在Getting Started中提到)可以找到Chrome的地址栏元素,其参数“value”包含当前的url。

我找到了两种获取此网址的方法:

from __future__ import print_function
from pywinauto import Desktop

chrome_window = Desktop(backend="uia").window(class_name_re='Chrome')
address_bar_wrapper = chrome_window['Google Chrome'].main.Edit.wrapper_object()

这是第一种方式:

url_1 = address_bar_wrapper.legacy_properties()['Value']

这是第二个:

url_2 = address_bar_wrapper.iface_value.CurrentValue

print(url_1)
print(url_2)

此外,如果协议是“http”,Chrome 会删除“http://”前缀。你可以添加类似的东西:

def format_url(url):
    if url and not url.startswith("https://"): 
        return "http://" + url
    return url

【讨论】:

  • 我们能得到更新吗?不适用于新的 chrome 版本
猜你喜欢
  • 2021-08-02
  • 1970-01-01
  • 2014-01-27
  • 2013-09-24
  • 1970-01-01
  • 1970-01-01
  • 2019-04-10
  • 2015-12-04
  • 2011-09-21
相关资源
最近更新 更多