0

我正在尝试从所有正在运行的 IE 实例中获取一些数据以从页面源中挂钩记录的用户名,我尝试使用 selenium 让用户从 selenium 运行 IE,然后当用户登录时我得到用户名,它有点工作仅在第一个选项卡中,如果用户从另一个窗口/选项卡转到登录页面,我无法理解。看起来带有 IE window_handles 的 selenium 不能很好地工作。在谷歌上搜索我可能会发现对我来说更好的东西,它可以从所有正在运行的选项卡/窗口中的股票 Internet Explorer 中获取一些数据。在这个链接之后,我能够获得页面标题和位置 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 * '-')
4

1 回答 1

0

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

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 可以看到可以设置输入值或单击按钮等。

于 2019-04-09T12:11:31.490 回答