最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to select element in Protractor by html attribute when value contains - Stack Overflow

programmeradmin2浏览0评论

Struggling with how to select an element that doesn't have a standard unique id or class. How would I select this input element with Protractor?

Note: I cannot use the class ComboBoxInput_Default class, as this drop down box is used on several other page elements. There also isn't any easily identifiable parent element for at least 10+ DOM levels.

<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" autoplete="off">    
<div>

The only identifying markup that makes each of these inputs different is appended to the end of the generated id, Solution_MultiComboSelection_Input.

If I had to get this element with jquery, I would use the (not preferrable) contains $( "input[name*='Solution_MultiComboSelection_Input']" ). Is there some parable way to locate elements in this way with Protractor?

Struggling with how to select an element that doesn't have a standard unique id or class. How would I select this input element with Protractor?

Note: I cannot use the class ComboBoxInput_Default class, as this drop down box is used on several other page elements. There also isn't any easily identifiable parent element for at least 10+ DOM levels.

<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" autoplete="off">    
<div>

The only identifying markup that makes each of these inputs different is appended to the end of the generated id, Solution_MultiComboSelection_Input.

If I had to get this element with jquery, I would use the (not preferrable) contains $( "input[name*='Solution_MultiComboSelection_Input']" ). Is there some parable way to locate elements in this way with Protractor?

Share edited Aug 18, 2015 at 16:28 alecxe 475k127 gold badges1.1k silver badges1.2k bronze badges asked Aug 17, 2015 at 21:03 BrianBrian 4651 gold badge5 silver badges10 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

Sure, use the "contains" or "ends-with" CSS selector:

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

If that is the only element with ComboBoxInput_Default as the class, then you could select it using element(by.css('.ComboBoxInput_Default'))

This page has a lot of examples for selectors.

Hard to know for sure without seeing the rest of the page but I'd try...

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

or maybe

$('div.ComboBoxInput_Default input.ComboBoxInput_Default);

That said, the best solution is to have an identifier added to the code. Hope this helps!

发布评论

评论列表(0)

  1. 暂无评论