How To Click On a Date in Web Table

Hi Everyone!

I want to click on a date in a web-table where the dates can be or can’t be unique. If the dates are not unique then I should be able to click the first occurrence of that date and if the date is unique then it should click on it. Below is the sample table html:

Next Travel Date Type Amount
Apr. 11, 2024 Cash $666.00
Jun. 11, 2024 Credit $555.00
Apr. 11, 2024 Cash $666.00

This is my code:

*** Variables ***
${ExternalDate}     Apr. 11, 2024

*** Test Cases ***
Click on Date in Table
    Wait Until Page Contains Element    xpath://table[@id='regularTravelExpense']
    ${date_elements}    Get WebElements    xpath://table[@id='regularInvestment']//td[@class='nextTravelDate']
    ${date_count}    Get Length    ${date_elements}
    ${date_found}    Set Variable    ${False}
    ${index}    Set Variable    ${-1}
    FOR    ${i}    IN RANGE    ${date_count}
        ${date}    Get Text    ${date_elements[${i}]}
        Run Keyword If    '${date}' == '${ExternalDate}'    Run Keyword If    '${date_found}' != '${True}'  Run Keywords    Set Variable    ${date_found}    ${True}    AND    Set Variable    ${index}    ${i}    AND    Exit For Loop
        Run Keyword If    '${date}' != '${ExternalDate}'    Run Keywords    Click Element    ${date_elements[${i}]}    AND    Exit For Loop
    END
    Run Keyword If    '${index}' != '-1'    Run Keywords    Click Element    ${date_elements[${index}]}    AND    ELSE    Log    No matching date found. 

The result I’m getting is: No matching date found

Can anyone guide me where I’m making mistake in my code?

Hi Gautam,

First step in troubleshooting is to log the variables and see what they actually contain when the robot runs (is it what you expected or something else?), The two most useful keywords are Log and Log To Console, my preference is if you want the variable output to the console use Log with console=True so you always have the output in the log.html as well.

Second I suspect something’s gone awry when you combined Run Keyword If and Run Keywords, the help for Run Keyword If actually recommends that you use the new IF / ELSE syntax, this will also help in troubleshooting.

*** Variables ***
${ExternalDate}     Apr. 11, 2024

*** Test Cases ***
Click on Date in Table
    Wait Until Page Contains Element    xpath://table[@id='regularTravelExpense']
    ${date_elements}    Get WebElements    xpath://table[@id='regularInvestment']//td[@class='nextTravelDate']
    ${date_count}    Get Length    ${date_elements}
    ${date_found}    Set Variable    ${False}
    ${index}    Set Variable    ${-1}
    FOR    ${i}    IN RANGE    ${date_count}
        ${date}    Get Text    ${date_elements[${i}]}
        Log         ${date}
        IF    '${date}' == '${ExternalDate}'
            IF    '${date_found}' != '${True}'  
                Set Variable    ${date_found}    ${True}
                Set Variable    ${index}    ${i}    
                Log         ${index}
                Exit For Loop
            END
        END
        IF    '${date}' != '${ExternalDate}'
            Click Element    ${date_elements[${i}]}
            Exit For Loop
        END
    END
    IF    '${index}' != '-1'
        Click Element    ${date_elements[${index}]}
    ELSE    
        Log    No matching date found.
    END

I think what might be happening is on the first iteration around the loop Run Keyword If '${date}' != '${ExternalDate}' this evaluates as true and the loop exits, I’m guessing you are possibly hitting Next Travel Date != Apr. 11, 2024

Dave.