Python shell Windows Internet Explorer获取页面Sou

2024-10-03 06:20:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图从所有正在运行的IE实例中获取一些数据,从页面源中钩住登录的用户名,我尝试使用selenium让用户从selenium运行IE,然后当用户登录时,我得到用户名,它只在第一个选项卡中工作,如果用户从另一个窗口/选项卡进入登录页面,我就无法捕捉到。看起来带有IE窗口手柄的selenium不能很好地工作。 在google上搜索,我可能找到了一个更好的东西,可以从所有运行的标签/窗口中的股票internetexplorer中获取一些数据。 在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 * '-')

Tags: 数据用户fromimporturlselenium页面sw
1条回答
网友
1楼 · 发布于 2024-10-03 06:20:07

我从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"]

正在分析文件国际电工委员会您可以看到您可以设置输入值或单击按钮等

相关问题 更多 >