javascriptpythonrobotframeworkselenium2library

Is there a way to pass a WebElement to javascript in Robot?


Is there a way to pass a WebElement to javascript in Robot? My script:

${el} =    Get Webelement    ${locator}
Execute Javascript    ${el}.checked = true;    #not sure how to approach this bit

I cannot use document.getElementById("whatever") as in some certain cases there's no ID used but custom locators.

Many thanks for your help!


Solution

  • So yes, I've got the solution by combination of above:

    Select Checkbox
        [Arguments]    ${locator}    ${timeout}=${global_timeout}
        ${tempId} =    Generate Random String    8
        Wait Until Page Contains Element    locator=${locator}    timeout=${timeout}
        ${origId} =    Get Element Attribute    ${locator}@id
        Assign Id To Element    locator=${locator}    id=${tempId}
        Execute Javascript    document.getElementById("${tempId}").checked = true;
        ${locator} =    Replace String    string=${locator}    search_for=${origId}    replace_with=${tempId}
        Assign Id To Element    locator=${locator}    id=${origId}
    
    Unselect Checkbox
        [Arguments]    ${locator}    ${timeout}=${global_timeout}
        ${tempId} =    Generate Random String    8
        Wait Until Page Contains Element    locator=${locator}    timeout=${timeout}
        ${origId} =    Get Element Attribute    ${locator}@id
        Assign Id To Element    locator=${locator}    id=${tempId}
        Execute Javascript    document.getElementById("${tempId}").checked = false;
        ${locator} =    Replace String    string=${locator}    search_for=${origId}    replace_with=${tempId}
        Assign Id To Element    locator=${locator}    id=${origId}
    
    Checkbox Should Be Selected
        [Arguments]    ${locator}    ${timeout}=${global_timeout}
        Wait Until Page Contains Element    locator=${locator}    timeout=${timeout}
        ${el} =    Get Webelement    ${locator}
        Should Be True    ${el.is_selected()}
    
    Checkbox Should Not Be Selected
        [Arguments]    ${locator}    ${timeout}=${global_timeout}
        Wait Until Page Contains Element    locator=${locator}    timeout=${timeout}
        ${el} =    Get Webelement    ${locator}
        Should Not Be True    ${el.is_selected()}