Search code examples
vbaseleniumselenium-webdriverclassnameinvalidselectorexception

InvalidSelector Error: compound class names not permitted using "FindElementByClass"


I am having trouble with code while trying to take the class name. I have tried and run time error 32 is appearing as:

InvalidSelector Error: compund class names not permitted

Maybe somebody canhelp with the code below. I have chrome browser 103.5060.114 but web driver 103.5060.56 . I could not find other driver to wnload.

HTML:

<span dir="auto" title="Customer" class="ggj6brxn gfz4du6o r7fjleex g0rxnol2 lhj4utae le5p0ye3 l7jjieqr i0jNr">Customer</span>

VBA code:

Sub iselementpresenttest()
    
Dim bot As New WebDriver
Dim ks As New Keys

'Init New Chrome instance & navigate to WebWhatsApp
bot.Start "chrome", "https://web.whatsapp.com/"
bot.Get "/"
bot.Window.Maximize
MsgBox "Please scan the QR code. After you are logged in, please confirm this message box by clicking 'ok'"
bot.Wait (3500)
        
'        If bot.FindElementByClass("_2qo4q _3NIfV") = 1 Then
'    Debug.Print "true"
'End If
        
        If bot.FindElementByClass("ggj6brxn gfz4du6o r7fjleex g0rxnol2 lhj4utae le5p0ye3 l7jjieqr i0jNr") = 1 Then
'                If bot.FindElementByXPath("//*[@id='pane-side']/div/div/div/div[11]/div/div/div[2]/div[2]/div[1]/span/div/span") = 1 Then
    Debug.Print "true"
End If
'        If bot.FindElementsByXPath("//*[@id='main']/div[3]/div/div[2]/div[3]/div[20]/div/div[1]/div[1]/div[2]/div/div/span").Count > 0 Then
    If bot.FindElementsByXPath("//*[@id='main']/div[3]/div/div[2]/div[3]/div[20]/div/div[1]/div[1]/div[2]/div/div/span").Count > 0 Then
        bot.TakeScreenshot.ToExcel.PasteSpecial (xlPasteAll)
        
            bot.Quit
            MsgBox "Yes"
        Else
            bot.Quit
            MsgBox "NO"
        End If
        
    End Sub

My aim is to take the send receive whatsapp icon to the excel sheet


Solution

  • You need to take care of a couple of things here:

    • The classnames of the <span> looks dynamic and and may change sooner or later, even may be next time you access the application afresh.
    • Selenium doesn't permit compund class names

    Solution

    To identify the element you can use the following locator strategies:

    • Using FindElementByCss:

      bot.FindElementByCss("span[title='Customer']")
      
    • Using FindElementByXPath:

      bot.FindElementByXPath("//span[@title='Customer' and text()='Customer']")