简体   繁体   English

遍历父元素并选择子元素

[英]Iterate over a parent element and select a child element

I have the following element which has number of child elements. 我有以下具有多个子元素的元素。 I want to write a function in such a way that my test clicks the mentioned option. 我想编写一个函数,使我的测试单击上述选项。

 navigateMenu: function(name){ var menu = $$('.nav-menu'); for(var i=0 ; i < menu.length ; i++){ console.log(menu[i].getText()); if(menu[i].getText() == name){ menu[i].click(); browser.options.sendKey('ENTER'); } } } 
  <div class='nav-menu'> <div class='nav-button'> <div class='button-text'>Option1</div> <div class='nav-button'> <div class='button-text'>Option2</div> <div class='nav-button'> <div class='button-text'>Option3</div> <div class='nav-button'> <div class='button-text'>Option4</div> <div class='nav-button'> <div class='button-text'>Option5</div> <div class='nav-button'> <div class='button-text'>Option6</div> </div> 

I am able to print all the values but unable to click and go to the given value. 我能够打印所有值,但无法单击并转到给定值。 What am I missing here ? 我在这里想念什么?

You are using the wrong element / selector. 您使用了错误的元素/选择器。 You need to be getting all the individual options not the whole menu. 您需要获取所有单个选项,而不是整个菜单。 If you log the menu element you are grabbing to the console you will see there is only one element in the array. 如果您将菜单元素记录到控制台,则会看到数组中只有一个元素。 So when you are calling getText() It is dumping all the text at once because it is all contained in that nav-menu element. 因此,当您调用getText()它会一次转储所有文本,因为所有文本都包含在该nav-menu元素中。

var menu = $$('.nav-menu');
console.log(menu);

Will give you a result similar to this one: 会给你类似的结果:

[ { ELEMENT: '0.10790115528393929-1',
'element-6066-11e4-a52e-4f735466cecf': '0.10790115528393929-1',
selector: '.nav-menu',
value: { ELEMENT: '0.10790115528393929-1' },
index: 0 } ]

Notice how there is only one element in the results array. 请注意,结果数组中只有一个元素。 If you change the selector though you can get a list of the options and then you should be able to click on each one. 如果您更改选择器,则可以获得选项列表,然后应该可以单击每个选项。 You can just explicitly get all the options at once. 您可以一次明确地获取所有选项。 All of the following examples will work for that. 以下所有示例均适用于此。

var options = $$('.nav-button')
var options = $$('.nav-menu > .nav-button')
var options = $$('.button-text')

Using one of these selectors now you will have more than one element in your array and then you should be able to iterate over each one and click on them. 现在使用这些选择器之一,您将在数组中拥有多个元素,然后应该可以遍历每个元素并单击它们。 I modified your original code snippet to get each option and logged the results to the console. 我修改了原始代码段以获取每个选项,并将结果记录到控制台。 You can see in the console log that it found the option I was looking for and that there were several iterations not just one. 您可以在控制台日志中看到它找到了我正在寻找的选项,并且有多个迭代而不仅仅是一个。

var options = $$('.nav-button');
console.log(options);

Gives you an array with 6 elements in it 给您一个包含6个元素的数组

[ { ELEMENT: '0.9727932413621352-1',
    'element-6066-11e4-a52e-4f735466cecf': '0.9727932413621352-1',
    selector: '.nav-button',
    value: { ELEMENT: '0.9727932413621352-1' },
    index: 0 },
  { ELEMENT: '0.9727932413621352-2',
    'element-6066-11e4-a52e-4f735466cecf': '0.9727932413621352-2',
    selector: '.nav-button',
    value: { ELEMENT: '0.9727932413621352-2' },
    index: 1 },
  { ELEMENT: '0.9727932413621352-3',
    'element-6066-11e4-a52e-4f735466cecf': '0.9727932413621352-3',
    selector: '.nav-button',
    value: { ELEMENT: '0.9727932413621352-3' },
    index: 2 },
  { ELEMENT: '0.9727932413621352-4',
    'element-6066-11e4-a52e-4f735466cecf': '0.9727932413621352-4',
    selector: '.nav-button',
    value: { ELEMENT: '0.9727932413621352-4' },
    index: 3 },
  { ELEMENT: '0.9727932413621352-5',
    'element-6066-11e4-a52e-4f735466cecf': '0.9727932413621352-5',
    selector: '.nav-button',
    value: { ELEMENT: '0.9727932413621352-5' },
    index: 4 },
  { ELEMENT: '0.9727932413621352-6',
      'element-6066-11e4-a52e-4f735466cecf': '0.9727932413621352-6',
      selector: '.nav-button',
      value: { ELEMENT: '0.9727932413621352-6' },
      index: 5 } 
]

And now if I use the loop, I can see that the option is actually found in the console messages and that there are several iterations of the loop. 现在,如果我使用循环,则可以看到实际上在控制台消息中找到了该选项,并且循环进行了多次迭代。

var name = 'Option2';

for(var i=0 ; i < options.length ; i++){
    console.log(`iteration ${i}: ${options[i].getText()}`);
    if(options[i].getText() === name){
       console.log('option found');
       options[i].click();
       browser.keys(['ENTER']);
   }
}

//console logs
iteration 0: Option1
iteration 1: Option2
option found
iteration 2: Option3
iteration 3: Option4
iteration 4: Option5
iteration 5: Option6

FInally, if you really need to get the parent element first and then iterate over each child you can do it like this. 最后,如果您确实确实需要先获取父元素,然后遍历每个孩子,则可以这样做。

var name = 'Option2';
var menu = $('.nav-menu');

browser.elementIdElements(menu.value.ELEMENT, '.nav-button').value.forEach(option => {
    console.log(browser.elementIdText(option.ELEMENT).value);

    var text = browser.elementIdText(option.ELEMENT).value;

    if(text === name) {
        console.log('option found');

        //do stuff...
    }        
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM