【问题标题】:Python ShellWindows Internet Explorer get Page SourcePython ShellWindows Internet Explorer 获取页面源
【发布时间】:2019-04-09 09:03:38
【问题描述】:

我正在尝试从所有正在运行的 IE 实例中获取一些数据,以从页面源中挂钩记录的用户名,我尝试使用 selenium 让用户从 selenium 运行 IE,然后当用户登录时我得到用户名和它仅在第一个选项卡中有效,如果用户从另一个窗口/选项卡转到登录页面,我无法理解。看起来带有 IE window_handles 的 selenium 不能很好地工作。 在谷歌上搜索我可能会发现对我来说更好的东西,它可以从所有正在运行的选项卡/窗口中的股票 Internet Explorer 中获取一些数据。 在this link 之后,我能够获得页面标题和位置 URL,它还解释了如何使页面导航到另一个 URL。 现在我找不到的是如何获取页面源。

from win32com.client import Dispatch 
from win32gui import GetClassName

ShellWindowsCLSID = '{9BA05972-F6A8-11CF-A442-00A0C90A8F39}'
ShellWindows = Dispatch ( ShellWindowsCLSID )

for sw in ShellWindows :
    if GetClassName ( sw . HWND ) == 'IEFrame' :
        print(sw)
        print(sw.LocationName)
        print(sw.LocationURL)
        #sw.Document.Location = "http://python.org" navigating to another url
        print(50 * '-')

【问题讨论】:

    标签: python windows internet-explorer hook


    【解决方案1】:

    我从this link 找到了一个解决方案,所以基本上是为了获取页面源

    from win32com.client import Dispatch 
    from win32gui import GetClassName
    
    ShellWindowsCLSID = '{9BA05972-F6A8-11CF-A442-00A0C90A8F39}'
    ShellWindows = Dispatch ( ShellWindowsCLSID )
    
    for sw in ShellWindows :
        if GetClassName ( sw . HWND ) == 'IEFrame' :
            #print the source
            print(sw.Document.body.outerHTML)
    
            #get all elements
            elements = sw.Document.all
    
            #get all inputs
            inputs = [x for x in elements if x.tagName == "INPUT"] # tag name is always uppercase
    
            #get all buttons
            buttons = [x for x in elements if x.tagName == "BUTTON"]
    
            #get by class name
            rows = [x for x in elements if "row" in x.className.split(' ')]
    
            #get by id
            username = [x for x in elements if x.getAttribute("id") == "username"]
    

    分析IEC.py文件可以看到可以设置输入值或者点击按钮等。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-09-13
      • 2017-10-19
      • 2018-03-05
      • 2016-02-26
      • 2017-06-04
      • 2012-10-02
      • 1970-01-01
      相关资源
      最近更新 更多