【问题标题】:Is there a way to pass a WebElement to javascript in Robot?有没有办法将 WebElement 传递给 Robot 中的 javascript?
【发布时间】:2018-01-26 03:03:32
【问题描述】:

有没有办法将 WebElement 传递给 Robot 中的 javascript?我的脚本:

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

我不能使用 document.getElementById("whatever"),因为在某些情况下没有使用 ID,而是使用自定义定位器。

非常感谢您的帮助!

【问题讨论】:

    标签: javascript python robotframework selenium2library


    【解决方案1】:

    这是不可能的,但您可以尝试使用关键字 Assign Id To Element 为您定位的元素提供一个 id,然后使用 document.getElementById 在 javascript 中检索您的元素

    编辑:如果分配 Id 不是一个选项,您可以查看上面提到的关键字如何将 id 分配给元素并使用相同的技巧实现您自己的关键字,但要选中您的复选框。像这样的:

    element = self._element_find(locator, True, True)
    self._current_browser().execute_script("arguments[0].checked=true;", element)
    

    【讨论】:

    • 不确定这一点,因为屏幕上进程的其他部分可能依赖于元素 ID。
    • 那么也许您可以编写自己的关键字,使用与我提到的关键字相同的技术(将 Id 分配给元素)。查看 Selenium2Library 中的源代码,看看它如何将 web 元素传递给 javascript(arguments[0] 部分)。不幸的是,我认为Execute Javascript 不能让您按原样传递这些参数。
    【解决方案2】:

    是的,我已经通过以上组合得到了解决方案:

    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()}
    

    【讨论】:

      【解决方案3】:

      不,您不能将 web 元素传递给 javascript。它是一个 python 对象。

      但是,如果您想调用对象上的方法,则不需要使用 javascript。例如,要检查元素是否被选中,您可以执行${el.is_selected()}

      这里记录了可用的元素方法:http://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.remote.webelement

      【讨论】:

      • 完美,这似乎适用于检查复选框是否被选中 - 有没有办法使用类似的方法来选择它?
      • 或者可能,有没有办法在不点击的情况下选中该复选框?因为该元素不可见。基本上,我正在寻找一种如何替换的方法: document.getElementById("whatever").checked=true;在javascript中
      • 此答案中的纯硒调用,或${checked}= Run Keyword And Return Status Checkbox Should Be Selected ${locator}
      【解决方案4】:
      ${child}=    Execute Javascript    return arguments;     ARGUMENTS    ${child}    ${parent}
      

      您可以使用 ARGUMENTS 关键字将参数传递给 Executer,您可以尝试打印 ${child} 并查看

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-09-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-27
        相关资源
        最近更新 更多