GCuser99 / SeleniumVBA

A comprehensive Selenium wrapper for browser automation developed for MS Office VBA running in Windows

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

isElementPresent with Variable?

MathisPr opened this issue · comments

I want to fill the input elements in a webform with Data from an excel Sheet.
i can loop through a table of IDs to fill the input elements with data from another table but i would like to check before if the specific input element exists.

I want to use IsElementPresent(FindBy.ID(...)) but it only accepts an object and all i have is the ID as a string.

FindElementById works just fine with variables, but not IsElementPresent.

can IsElementPresent work with a variable for the ID?
if not, is there a workaround?

The code Section:

        IDType = ThisWorkbook.Worksheets("FieldIDs").Cells(RowNumFieldIDs, ColIDType)
        IDValue = ThisWorkbook.Worksheets("FieldIDs").Cells(RowNumFieldIDs, ColNumFieldIDs).Value
        CellValue = ThisWorkbook.Worksheets("DataInput").Cells(RowNumDataInput, ColNumDataInput).Value
             
        If IDType = "Input" Then   'works fine
            'If wb.IsElementPresent(FindBy.ID(FieldID), 1000) Then      'does not work
                wb.FindElementById(IDValue).SendKeys CellValue    'works fine
            'End If
        End If

Thx!

commented

Hi @MathisPr,

There is no method called IsElementPresent in the SeleniumVBA Object Model.

We do have a method called IsPresent which performs a similar function I think.

    driver.Wait 1000
    If driver.IsPresent(By.ID, IDValue) Then 
         driver.FindElementById(IDValue).SendKeys CellValue 
    End If

However, unlike your IsElementPresent, which appears to allow you to specify a maximum timeout value to wait for presence, IsPresent in SeleniumVBA checks for immediate presence (timeout=0), so that is why I added the explicit wait in my code above. Because my code will wait 1 second whether the element is present or not, it is likely to be slower than when using an implicit wait.

But your question raises an important issue with our IsPresent method. On the next version of SeleniumVBA, we will consider adding an optional implicit wait parameter to the IsPresent method, so that the explicit wait is not necessary.

Thanks for your question.

commented

@MathisPr, with SeleniumVBA version 3.1 you can now try:

    If driver.IsPresent(By.ID, IDValue, 1000) Then 
         driver.FindElementById(IDValue).SendKeys CellValue 
    End If

Thank you very much for the instant reply and the solution! I'm quite impressed how quickly you updated SeleniumVBA. I plan to switch my project from Seleniumbasic to SeleniumVBA altogether when i have the time. Keep up the great work!

commented

No problem @MathisPr! Be aware that there is now an ActiveX Dll available, in case you want to try that. It does not require a redistributable like .Net Framework, nor does it require a particular install location, and you get the bonus of automated driver/browser version alignment. It would be great to have someone moving from SeleniumBasic to put the Dll through a rigorous test challenge.