userxxxso
userxxxso

Reputation: 165

Selenium Basic VBA focus on new tab that is opened by Click

I'm working on the Selenium Basic Wrapper for Excel VBA, but upon clicking a button that opens a new tab, I am unable to get the selenium web-driver to switch focus onto the new tab that is opened, and to close the original tab.. Believe this should be achievable using the Java Selenium, but is there any way to do it through the Selenium Basic wrapper on excel VBA?

Have tried using bot.switchtopreviouswindow/nextwindow to no avail, Selenium does not even seem to detect the new tab/window opened as an existing window, also tried using switchwindow using title to no avail...

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

Just need Selenium to switch focus onto newly opened tab and for old/original tab to be closed..

Upvotes: 0

Views: 6798

Answers (2)

RWB
RWB

Reputation: 127

Sub DownloadFileFromLaquintaca2() Dim cd As New selenium.ChromeDriver Dim DefaultChromeDownloadFolder As String Dim MyURL As String

' 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

End Sub

Upvotes: 0

QHarr
QHarr

Reputation: 84465

The following works for me where you use switchToNextWindow and then you take 1 off the current window count (you might want to check more .windows.count > 1 first), and close that item in windows collection.

You can also .SwitchToWindowByName and .SwitchToWindowByTitle if either of those are known in advance | can be extracted from the current page.

'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

Upvotes: 2

Related Questions