【问题标题】:Accessing elements located by custom locator strategy with Javascript使用 Javascript 访问由自定义定位器策略定位的元素
【发布时间】:2018-01-26 00:33:46
【问题描述】:

我有以下问题:

  • 我有一个隐藏的复选框,还有另一个图层用来使它美观和闪亮
  • 我可以通过访问其 ID 轻松更改其值,但我还需要使用我的自定义定位器访问此元素,该定位器使用 xpath(必须保持可变)

所以,这是我的脚本:

Custom Select Checkbox    id=my_checkbox        #that works fine
Custom Select Checkbox    customLocatorStrat    #that doesn't work at all

*** Keywords ***
Custom Select Checkbox
    [Arguments]    ${locator}    ${timeout}=${global_timeout}
    Execute Javascript    document.getElementById("${resultLocator}").checked = true;
    OR
    ${el}.checked = true;     #need to get the ${el} variable if the ${locator is not an id}


My Custom Locator
    [Arguments]    ${criteria}    ${tag}    ${constraints}
    ...     #assembling xpath
    ${el}=    Get Webelements    xpath=${path}
    [Return]    ${el}

定位器策略没有问题 - 它运行良好。我只需要在我的自定义选择复选框关键字中使用/强制它。我需要获取 ${el} 变量,并且考虑到有多个自定义定位器关键字直接调用它对我不起作用。知道该怎么做吗?非常感谢。

【问题讨论】:

    标签: python robotframework selenium2library


    【解决方案1】:

    selenium2library 中的自定义定位器需要通过Add Location Strategy [Doc] 激活。在我看来,这似乎是您的示例中缺少的部分。

    *** Test Cases ***
    Test Case
        Add Location Strategy   custom  Custom Locator Strategy
        Page Should Contain Element custom=my_id
    
    *** Keywords ***
    Custom Locator Strategy 
        [Arguments]    ${browser}    ${criteria}    ${tag}    ${constraints}
        ${retVal}=    Execute Javascript    return
    window.document.getElementById('${criteria}');      
        [Return]    ${retVal}
    

    【讨论】:

      【解决方案2】:
      ${el}=     Run Keyword If  '${locator}'!='my_checkbox'    My Custom Locator   ${criteria}    ${tag}    ${constraints}
      

      在执行${el}.checked = true;之前可以得到变量

      【讨论】:

      • 问题是我不能调用定位器策略,因为有多个。
      • 然后代替[Return] ${el}......你可以将它设置为测试变量。
      • 那么如何在 Javascript 脚本中重用 web 元素 ${el}?
      • 如果我这样做:执行 Javascript ${el}.checked = true;它失败了
      • 所以如果 locator 是一个 id ..那么你需要运行 dis 执行 Javascript document.getElementById("${resultLocator}").checked = true; ....else 执行 Javascript ${el}.checked = true; .........对吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-28
      相关资源
      最近更新 更多