Home > Back-end >  Selenium VBA - Runtime Error 11 ElementyNotVisible Error element not interactable
Selenium VBA - Runtime Error 11 ElementyNotVisible Error element not interactable

Time:01-30

I am using the below mentioned code for automation of Edge Browser

The code is working fine except for "If then Else" block.

The complete script is as follows

Dim Obj As New WebDriver
' search for company - SF 1cr and above
Sub EdgeAutoSF1CRA0()
Set Obj = New Selenium.EdgeDriver
Dim ele As WebElement
Dim By As New Selenium.By
Obj.SetCapability "ms:edgeOptions", "{""excludeSwitches"":[""enable-automation""]}"
Obj.Start "edge", ""
Obj.Get "https://*********************"
Obj.Window.Maximize

    Obj.FindElementByName("croreAccount").SendKeys ("Search")
    Obj.FindElementByXPath("//*[@id='loadSuitFiledDataSearchAction']/div[1]/div[3]/div[4]/img").Click
        Obj.FindElementById("borrowerName").SendKeys (ThisWorkbook.Sheets("Sheet1").Range("C5").Value)
        Obj.FindElementByXPath("//*[@id='search-button']/ul/li[1]/div/input").Click
        Obj.Wait 30000

If Obj.FindElementByCss("#downloadReport").Attribute("Style" = "display") = "none" Then

 Obj.FindElementByXPath("//*[@id='three-icons']/ul/li[3]/a/div").Click
 Else
 Obj.FindElementByXPath("//*[@id='downloadReport']/div").Click

End If

End Sub

In the If then Else statement I want to search for the style attribute of the id "downloadReport" for "display :none"

The code on website is < a href="downloadStatusReport" id="downloadReport" style="display: none;"><div >Download</div></a>

However, code always evaluate the statement as False and proceeds to execute the command "Obj.FindElementByXPath("//*[@id='downloadReport']/div").Click"

CodePudding user response:

  1. The attribute name is style, not capitalized Style.
  2. You can construct the locator so that the desired style value will be a part of locator itself.
  3. You can use FindElements instead of FindElement so it will return you a list of matching elements so as if such element found it will return a non-empty list, otherwise it would be an empty list. With this you can check if returned list is empty or not, as following:
If Not IsEmpty(Obj.FindElementsByCss("[id='downloadReport'][style*='none']")) Then
    
 Obj.FindElementByXPath("//*[@id='three-icons']/ul/li[3]/a/div").Click
 Else
 Obj.FindElementByXPath("//*[@id='downloadReport']/div").Click
    
End If

CodePudding user response:

In short, there is no merit in locating an element with style attribute set as display: none;. Even through you locate the element, you won't be able to click on it. So the If-Then-Else logic won't work as intended.

Instead find the desired element which is visible and enabled element, so you can invoke click on it.

  • Related