How to check if an element is Interactable VBA

Macro_Nerd99

Board Regular
Joined
Nov 13, 2021
Messages
61
Office Version
  1. 365
I'm trying to prevent a "Element not interactable" runtime error in Selenium Basic.
I got this code from GhatGPT and it's laid out the way I wanted, but it doesn't seem to be using the correct method to find out if a web element is clickable.
Any suggestions??

VBA Code:
Public Sub Click_btn(Search_Value As String)
    Dim Findby As Selenium.By
    Set Findby = New Selenium.By

    Dim SearchElement As Selenium.WebElement
    Set SearchElement = Nothing

    ' Try to find the element by Name
    If ch.IsElementPresent(Findby.Name(Search_Value), 100) Then
        Set SearchElement = ch.FindElementByName(Search_Value)
    End If

    ' If the element is not found or found but not enabled by Name, try by Class
    If SearchElement Is Nothing Or (Not IsElementEnabled(SearchElement) And Not SearchElement Is Nothing) Then
        If ch.IsElementPresent(Findby.Class(Search_Value), 100) Then
            Set SearchElement = ch.FindElementByClass(Search_Value)
        End If
    End If

    ' If the element is not found or found but not enabled by Name, Class, and Id, try by Link Text
    If SearchElement Is Nothing Or (Not IsElementEnabled(SearchElement) And Not SearchElement Is Nothing) Then
        If ch.IsElementPresent(Findby.linktext(Search_Value), 100) Then
            Set SearchElement = ch.FindElementByLinkText(Search_Value)
        End If
    End If

    ' If the element is not found or found but not enabled by Name, Class, Id, and Link Text, try by Partial Link Text (containing)
    If SearchElement Is Nothing Or (Not IsElementEnabled(SearchElement) And Not SearchElement Is Nothing) Then
        If ch.IsElementPresent(Findby.partiallinktext(Search_Value), 100) Then
            Set SearchElement = ch.FindElementByPartialLinkText(Search_Value)
        End If
    End If

    ' Check if the element is found and enabled
    If Not SearchElement Is Nothing And IsElementEnabled(SearchElement) Then
        ' Click the element
        SearchElement.Click
    Else
        ' Handle the case where the element is not found or not enabled
        MsgBox "Element is not found or not enabled."
    End If
End Sub

Function IsElementEnabled(element As Selenium.WebElement) As Boolean
    ' Check if the element is enabled by other criteria (customize based on your use case)
    ' You may need to check other attributes, styles, or custom criteria to determine element enablement.
    ' The below logic is a placeholder and may not work for all cases.

    ' For example, you might check if the element is displayed and not grayed out.
    IsElementEnabled = element.Displayed And Not (element.GetCssValue("color") = "gray")
End Function
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.

Forum statistics

Threads
1,224,822
Messages
6,181,165
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top