【问题标题】:minimize window driver selenium excel vba最小化窗口驱动程序 selenium excel vba
【发布时间】:2019-05-22 22:40:58
【问题描述】:

我已经搜索了很多方法来最小化 selenium for excel vba 中的驱动程序窗口。我找到了 Java 和 python 的方法并尝试采用它们,但我所有的尝试都失败了 我刚刚找到了一种使用

来最大化窗口的方法
bot.Window.Maximize

但在尝试使用最小化时出现错误

我再次搜索 excel vba 和 selenium ... 感谢高级帮助

【问题讨论】:

    标签: excel vba selenium-webdriver web-scraping


    【解决方案1】:

    AFAIK 在 VBA 实现中没有这种方法(例如在 Python 中)。有很多方法可以控制大小和位置,例如

    bot.Window.SetSize 0, 0
    

    或者你可以无头运行

    bot.AddArgument "--headless"
    

    您也可以尝试:

    1) 模拟 Windows 键 + 向下

    2) 编写一个执行 window.minimize() 的 javscript 函数并异步执行父窗口

    3) 通过生成GetWindowPlacement 调用以及实现您自己的WINDOWPLACEMENT 结构来捕获您的目标坐标。看起来很快就变丑了。

    另见:

    Getting the size of a minimized window

    【讨论】:

    • 非常感谢。我已经测试了建议的两个代码,但都不能正常工作
    • 无头意味着没有浏览器可见。设置大小可以与设置位置一起使用,以放置您需要的位置。否则,你知道 python,我认为它有一个窗口最小化方法。
    • 我再次测试了无头部分,现在可以了。非常感谢
    • 该论点必须在开头。 :-)
    • Driver.Window.SetSize 0, 0 只是让窗口变小了,没有将浏览器最小化到任务栏。
    【解决方案2】:

    Driver.Window.SetSize 0, 0 只是缩小了窗口,没有将浏览器最小化到任务栏。

    如何在 selenium v​​ba 中使用GetWindowPlacement?

        'for vb6
    Private Type POINTAPI
            x As Long
            y As Long
    End Type
     
    Private Type RECT
            Left As Long
            Top As Long
            Right As Long
            Bottom As Long
    End Type
     
     
    Private Type WINDOWPLACEMENT
            Length As Long
            flags As Long
            showCmd As Long
            ptMinPosition As POINTAPI
            ptMaxPosition As POINTAPI
            rcNormalPosition As RECT
    End Type
     
    Private Declare Function GetWindowPlacement Lib "user32" (ByVal hwnd As Long, lpwndpl As WINDOWPLACEMENT) As Long
     
     
    Private Sub Command1_Click()
        Dim wp As WINDOWPLACEMENT
        wp.Length = Len(wp)
        GetWindowPlacement targetHandle, wp
    End Sub
    

    【讨论】:

    • 非常感谢。变量targetHandle 是什么? selenium驱动如何使用?
    • 在“通过 windows API 最小化窗口”下方查看我的答案
    【解决方案3】:

    通过 windows API 最小化窗口

    这是 Selenium VBA 没有有效的最小化窗口选项的解决方法。

    ''compiler constants
    #If VBA7 Then
        Public Declare PtrSafe Function ShowWindow Lib "user32" (ByVal hwnd As LongPtr, ByVal nCmdShow As Long) As Boolean
        Public Declare PtrSafe Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As LongPtr, ByVal lpString As String, ByVal cch As Long) As Long
        Public Declare PtrSafe Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As LongPtr, ByVal hWnd2 As LongPtr, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
    #Else
        Public Declare Function ShowWindow Lib "USER32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Boolean
        Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
        Public Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
    #End If
    
        Dim hwnd As Long
        Dim Botwindowtitle As String
        bot.Start    
        Botwindowtitle = bot.Window.Title
        hwnd = GetAllWindowHandles(Botwindowtitle)
        Call ShowWindow(hwnd, 7) 'Show the window minimized (SW_SHOWMINNOACTIVE = 7) http://www.jasinskionline.com/windowsapi/ref/s/showwindow.html
         bot.Get "https://www.google.com/"
    
    
    
    Private Function GetAllWindowHandles(partialName As String) As Long
        Dim hwnd As Long, lngRet As Long
        Dim strText As String
        Dim hWndTemp As Long
        hwnd = FindWindowEx(0&, 0&, vbNullString, vbNullString)
    
    Do While hwnd <> 0
            strText = String$(100, Chr$(0))
            lngRet = GetWindowText(hwnd, strText, 100)
    
            If InStr(1, strText, partialName, vbTextCompare) > 0 Then
                Debug.Print "Window Handle:" & hwnd & vbNewLine & _
                            "Window title:" & Left$(strText, lngRet) & vbNewLine & _
                            "----------------------"
                hWndTemp = hwnd
                GetAllWindowHandles = hWndTemp
            End If
    
            '~~> Find next window
            hwnd = FindWindowEx(0&, hwnd, vbNullString, vbNullString)
    Loop
    End Function
    

    【讨论】:

    • 非常感谢。我已经测试了代码,但浏览器没有最小化。
    • 似乎只能使用 GetAllWindowHandles 函数通过标题获取句柄。立即尝试。
    • 同样的问题。代码有效,但窗口未最小化。
    • 你得到 hwnd0 了吗?如果这是真的,请尝试调用 ShowWindow(hwnd, 11)。我试过了,它确实最小化了。
    • 我试过这条线并且工作了hwnd = GetAllWindowHandles("data:, - Google Chrome"),因为似乎Botwindowtitle = bot.Window.Title返回空字符串。
    猜你喜欢
    • 2020-12-26
    • 1970-01-01
    • 2018-07-15
    • 1970-01-01
    • 2012-07-06
    • 1970-01-01
    • 2017-07-27
    • 2018-11-23
    • 1970-01-01
    相关资源
    最近更新 更多