Search code examples
vbscripthp-uft

How to check if a value exists on a webpage with visual basic script


I have been looking for a way to check a webpage for a date, if the date is there, then I want to progress with the loop otherwise exit the loop. I want to try and accomplish something like the below (please excuse my code, I am just starting out). However this does not seem to be the correct:

Dim DateToClick 
DateToClick = today

For i = 1 To 3
    '... some code ...

    If Browser("somebrowser").Page("somepage").Frame("someframe").WebTable("sometable").DateToClick.Exists = True 
    Then
        Browser("somebrowser").Page("somepage").Frame("someframe").WebTable("sometable").DateToClick.click 
    Else Exit For

        '... some more code ...

    End If

Is there any function within VB designed to check a web page for a value? Any help would be greatly appreciated.


Solution

  • From the code you have demonstrated, it looks as though you are searching through a WebTable object. In general, the easiest way to check if a value appears in such an object is to use the built in GetRowWithCellText method.

    myRow = Browser("").Page("").Frame("").WebTable("myTable").GetRowWithCellText("20/09/2016")
    

    The above code would set myRow to the number of the first row found containing specific text (in this case "20/09/2016"). You can use this row to then interrogate the rest of the table columns looking for any related data. As you've indicated that you want to click on the item if it exists, I'd guess that the WebTable contains links. If that is the case then you can simply set an object reference and then click it, like so:

    Set myLink = Browser("").Page("").Frame("").WebTable("myTable").ChildItem(myRow, myCol, "Link", 0)
    myLink.Click
    

    You would need to set myCol in the above example to the column number the date appears in (indexed from 1). What this does is creates an object that points to the first object of type Link held within the WebTable cell described by myRow and myCol, and then clicks on it.

    Let me know if you require any further clarification, or if this isn't quite what you needed and I'll try to assist more.