Macro_Nerd99
Board Regular
- Joined
- Nov 13, 2021
- Messages
- 61
- Office Version
- 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??
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