【问题标题】:Selenium Basic VBA focus on new tab that is opened by ClickSelenium Basic VBA 专注于单击打开的新选项卡
【发布时间】:2019-05-26 14:37:18
【问题描述】:

我正在开发用于 Excel VBA 的 Selenium Basic Wrapper,但是在单击打开新选项卡的按钮时,我无法让 selenium Web 驱动程序将焦点切换到打开的新选项卡上,并且关闭原始选项卡.. 相信这应该可以使用 Java Selenium 来实现,但是有没有办法通过 excel VBA 上的 Selenium Basic 包装器来做到这一点?

尝试使用 bot.switchtopreviouswindow/nextwindow 无济于事,Selenium 甚至似乎都没有检测到作为现有窗口打开的新选项卡/窗口,还尝试使用带有标题的 switchwindow 无济于事...

Private Sub CommandButton1_Click()

    Dim bot As New ChromeDriver
    Dim merchant As String
    Dim promocode As Object
    Dim number As Long
    Dim test As Object
    Dim testnumber As Integer

    lastColumn = Sheets("Merchants").Range("B" & Rows.Count).End(xlUp).Row

    For i = 2 To lastColumn

        bot.Get (Sheets("Merchants").Range("B" & i).Value)
        merchant = Sheets("Merchants").Range("A" & i).Value

        For Each promocode In bot.FindElementByClass("main_vouchers").FindElementsByClass("c")

            number = Right(promocode.Attribute("id"), 6)

            If IsEmpty(promocode) = True Then Exit For


            promocode.FindElementByClass("go").Click

            #This is the part I have problems with as after click, original page re-directs to another page, and new tab opens (which is the place  I want focus on). Also, I need the original tab closed so that Chrome doesn't end up opening too many tabs due to the loop running. Appreciate the help!

        Next promocode

    Next i

End Sub

只需要 Selenium 将焦点切换到新打开的选项卡并关闭旧/原始选项卡..

【问题讨论】:

  • 标签会有不同的名称可以使用吗?

标签: excel vba selenium selenium-webdriver selenium-chromedriver


【解决方案1】:

以下对我有用,您使用switchToNextWindow,然后从当前窗口计数中减去 1(您可能需要先检查更多 .windows.count > 1),然后关闭 windows 集合中的该项目。

如果事先知道其中任何一个,您也可以使用 .SwitchToWindowByName 和 .SwitchToWindowByTitle |可以从当前页面中提取出来。

'Ensure latest applicable driver e.g. ChromeDriver.exe in Selenium folder
'VBE > Tools > References > Add reference to selenium type library
Public Sub Example()
    Dim d As WebDriver
    Set d = New ChromeDriver
    Const URL = "https://www.cuponation.com.sg/agoda-discount-code"

    With d
        .get URL
        Debug.Print .Window.Title
        .FindElementByCss(".go").Click
        .SwitchToNextWindow
        Debug.Print .URL   '<= includes voucher code
        'do something with new window
        Debug.Print .Window.Title
        .Windows.item(.Windows.Count - 1).Close 'close prior window
        Debug.Print .Window.Title
        Stop
        .Quit
    End With
End Sub

【讨论】:

  • 嗨 QHarr,我试过 SwitchToNextWindow,但是,它不起作用,因为新选项卡是由单击操作触发的,因此 SwitchToNextWindow 不会将 Selenium 焦点带到打开的新 Chrome 选项卡通过点击动作。顺便说一句,我正在处理的 URL 是 cuponation.com.sg/agoda-discount-code 当我让 Selenium 单击“查看代码”按钮时,它会自动打开一个新选项卡,并且旧选项卡会重新加载到其他内容中......我想切换焦点到打开的新选项卡上并关闭原始选项卡。谢谢!
  • 另外,即使我实际上有新标签的标题,使用 .SwitchToWindowByTitle 也不起作用......似乎 Selenium 没有检测到点击操作打开的新标签甚至在那里...... .
【解决方案2】:

Sub DownloadFileFromLaquintaca2() 将 cd 调暗为新的 selenium.ChromeDriver 将 DefaultChromeDownloadFolder 调暗为字符串 将 MyURL 调暗为字符串

' Start Chrome
Set cd = New ChromeDriver
cd.Start

' Navigate to
cd.get "https://www.laquintaca.gov/connect/short-term-vacation-rentals"

Const URL = "https://www.cuponation.com.sg/agoda-discount-code"

Dim FindBy As New selenium.By
Dim imgElement As selenium.WebElement
        
' Check if element is present with CSS
If Not cd.IsElementPresent(FindBy.Css("img[alt='ACTIVE & SUSPENDED PERMITS BOX']")) Then
    MsgBox "Could not find image box"
   Exit Sub
End If

cd.FindElementByCss("img[alt='ACTIVE & SUSPENDED PERMITS BOX']").Click
   
Application.Wait (Now + TimeValue("0:00:3"))

'Get URL Address in Second Chorme Tab
      With cd
        .get URL
        .SwitchToNextWindow
        MyURL = .URL
        'Debug.Print .Window.Title
        'Debug.Print myURL
        .Windows.Item(.Windows.Count - 1).Close 'close prior window
    End With

'Write PDF File into Location with file Name STVR.PDF ---D:\MyDownLoads\STVR.pdf
Dim WinHttpReq As Object
Set WinHttpReq = CreateObject("Microsoft.XMLHTTP")
WinHttpReq.Open "GET", MyURL, False, "username", "password"
WinHttpReq.send

If WinHttpReq.Status = 200 Then
    Set oStream = CreateObject("ADODB.Stream")
    oStream.Open
    oStream.Type = 1
    oStream.Write WinHttpReq.responseBody
    oStream.SaveToFile "D:\MyDownLoads\STVR.pdf", 2 ' 1 = no overwrite, 2 = overwrite
    oStream.Close
End If

结束子

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-06
    • 2018-04-29
    • 2016-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多