【问题标题】:How to select element in Protractor by html attribute when value contains值包含时如何通过html属性选择量角器中的元素
【发布时间】:2015-08-17 21:03:02
【问题描述】:

苦于如何选择没有标准唯一 ID 或类的元素。我将如何使用量角器选择此输入元素?

注意:我不能使用ComboBoxInput_Default 类,因为这个下拉框用于其他几个页面元素。至少 10 多个 DOM 级别也没有任何易于识别的父元素。

<div style="display:inline; white-space: nowrap;" id="ctl00_ctl31_g_b56afa08_7869_450c_8871_f6759a89d9b1_ctl00_WPQ3txtFields_ddPositioList_10_Solution_MultiComboSelection" class="ComboBox_Default">
  <input type="text" style="width: 133px; height: 15px;" delimiter=";" class="ComboBoxInput_Default" value="-select-" name="ctl00$ctl31$g_b56afa08_7869_450c_8871_f6759a89d9b1$ctl00$WPQ3txtFields_ddPositioList_10_Solution_MultiComboSelection_Input" id="ctl00_ctl31_g_b56afa08_7869_450c_8871_f6759a89d9b1_ctl00_WPQ3txtFields_ddPositioList_10_Solution_MultiComboSelection_Input" autocomplete="off">    
<div>

使这些输入中的每一个不同的唯一标识标记附加到生成的 id 的末尾,Solution_MultiComboSelection_Input

如果我必须用 jquery 获取这个元素,我会使用(不推荐)包含$( "input[name*='Solution_MultiComboSelection_Input']" )。是否有类似的方法可以使用 Protractor 以这种方式定位元素?

【问题讨论】:

    标签: javascript selenium testing protractor end-to-end


    【解决方案1】:

    当然,使用“包含”或“结束于”CSS 选择器:

    element(by.css("input[id*=Solution_MultiComboSelection_Input]"));
    element(by.css("input[id$=Solution_MultiComboSelection_Input]"));
    

    【讨论】:

    • 哈!好 Alecxe... 不知道我们能做到这一点。 #有帮助
    • 啊,太好了,谢谢!我会将此标记为答案。
    【解决方案2】:

    如果这是唯一一个以 ComboBoxInput_Default 作为类的元素,那么您可以使用 元素(by.css('.ComboBoxInput_Default'))

    This page 有很多选择器的例子。

    【讨论】:

    • 欢迎来到 Stack Overflow!只是一个友好的提醒,以确保您的答案可以独立存在,并且不要依赖外部链接来完成。
    【解决方案3】:

    如果没有看到页面的其余部分,很难确定,但我会尝试...

    $('input[value="-select-"].ComboBoxInput_Default);
    

    或许

    $('div.ComboBoxInput_Default input.ComboBoxInput_Default);
    

    也就是说,最好的解决方案是在代码中添加一个标识符。希望这会有所帮助!

    【讨论】:

    • 谢谢,盐水!抱歉,我无法通过 css 类 ComboBoxInput_Default 进行选择,因为同一类用于页面上的其他 5 个输入。
    • 啊,是的,这很难 :) 我喜欢 Alecxe 的回答……希望对你有用!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-30
    • 2012-03-10
    • 2011-10-10
    • 1970-01-01
    • 2014-07-01
    • 2016-03-25
    • 2014-01-10
    相关资源
    最近更新 更多