简体   繁体   English

如何在元素上通过by.css使用子选择器>(或+等)?

[英]How can I use child selector > (or + etc.) with by.css on an element?

Given: 鉴于:

var someDivElement = element(by.css('div.main'));

Now, I would like to select further child elements on someDivElement , but with child selector. 现在,我想在someDivElement上选择其他子元素,但要使用子选择器。 Unfortunately, the child selector won't work that way: 不幸的是,子选择器不会那样工作:

var childElement = someDivElement.element(by.css('> section > span'));

This will result in an invalid css path error . 这将导致无效的CSS路径错误 Is there a workaround? 有解决方法吗? What I dont wan't to do is following: 我不想做的是以下几点:

var childElement = element(by.css(someDivElement.locator().value + '> section > span'));

UPDATE: Html-Code looks something like this: 更新:HTML代码看起来像这样:

<div class="main">
    <section>
        <span></span> <!-- I want ONLY this span -->
        <div class="main">
             <section>
                 <span></span> <!-- this span i don't want -->
             </section>
         </div>
    </section>
</div>

If I do as suggested: 如果我按照建议做:

var childElement = someDivElement.element(by.css('div.main > section > span'));

I still end up with a warning more than one element found for locator By.cssSelector 我仍然收到警告, more than one element found for locator By.cssSelector

  1. One way of doing this is to get all the child elements under parent element using chaining technique and then use get() or first() methods to get the element (You can even use filter() or map() function to get the precise element too) . 一种方法是使用链接技术将所有子元素放在父元素下,然后使用get()first()方法来获取元素(您甚至可以使用filter()map()函数来获取精确的元素也是如此) Here's how - 这是如何做 -

     var someDivElement = element.all(by.css('div.main')).first(); var childElement = someDivElement.$$(by.css('section > span')).first(); //OR var someDivElement = $$('div.main').first(); var childElement = someDivElement.$$(by.css('section > span')).get(0); 
  2. If you are looking for css selectors that can do this work, then you can do it this way - 如果您正在寻找可以完成这项工作的CSS选择器,则可以通过以下方式做到这一点-

     var childElement = $('div.main:nth-of-type(even) > section >span'); 

nth-of-type(even) function will return the first div. nth-of-type(even)函数将返回第一个div。 If you want to get the second div then use odd instead of even. 如果要获取第二个div,请使用奇数而不是偶数。 You can also provide numbers to the function. 您还可以为该功能提供数字。

  1. Other way is to get the locator value of the parent element which is an ElementFinder and then use it. 另一种方法是获取作为ElementFinder的父元素的定位符值,然后使用它。 Here's how - 这是如何做 -

     var childElement = element.all(by.css(someDivElement.elementArrayFinder_.locator_.value + '> section > span')).first(); 
  2. You can even use the locator() function that you have mentioned in your question using first() or get() function. 您甚至可以使用first()get()函数来使用在问题中提到的locator() get()函数。


EDIT: To avoid IndexOutOfBound Exception in the case of first() and get() functions, you can write a custom wait() function to see if the element count is greater than 0 and then proceed with operations. 编辑:为了避免在first()get()函数的情况下IndexOutOfBound Exception ,您可以编写一个自定义的wait()函数以查看元素计数是否大于0,然后继续进行操作。 Here's how - 这是如何做 -

var someDivElement = element.all(by.css('div.main'));
var childElement;
browser.wait(function(){
    return someDivElement.count().then(function(count){
        return count > 0;
    });
}, 10000); //Wait for 10 seconds before timing out
childElement = someDivElement.$$(by.css('section > span')).first();

Hope it helps. 希望能帮助到你。

var childElement = someDivElement.element(by.css('section'));

or in a page file this might look like: 或页面文件中的内容可能类似于:

someDivElement:{get: function(){ return element(by.css('div.main')); }}
childElement:  {get: function(){ return this.someDivElement.element(by.css('section')) }},

In your case, you need to add a wrapper element where you will define your own selector ( For example, this ). 在您的情况下,您需要添加一个wrapper元素,在其中您将定义自己的选择器( 例如this )。 Suppose, the selector this will point to the current element. 假设,选择this将指向当前元素。

The custom selector function will be something like this: 自定义选择器功能将如下所示:

function myCustomSelector(referenceElement, selector){
  //
  var targetElement = element(by.css(selector));
  // if selector has "this" reference
  if(/^this\s/.test(selector)){
    referenceElement.setAttribute('temporary-reference-attribute','1');
    selector = selector.replace(/^this/,'[temporary-reference-attribute]');
    targetElement = element(by.css(selector));
    referenceElement.removeAttribute('temporary-reference-attribute');
  }
  return targetElement;
}

And then call: 然后调用:

var childElement = myCustomSelector(someDivElement, 'this > section');

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

相关问题 我如何从最后一个元素而不是第一个元素开始使用:nth-​​child()选择器? - How can I use the :nth-child() selector starting from the last element, rather than the first? 单击元素时如何显示下3个,6个,9个等子级? - How can I show the next 3, 6, 9, etc. children when clicking an element? 如何使用&#39;this&#39;的子元素作为jQuery中的选择器? - How do I use the child element of 'this' as a selector in jQuery? 给定一个bySelector,我如何获得该元素的孩子的选择器? - given a bySelector how can i get the selector of this element's child? 如何将变量与 nth-child 选择器一起使用? - How can I use variables with nth-child selector? 如何获得第二行(第三行等)的缩进,以使用CSS使用自定义编号匹配列表中的第一行? - How can I get second (third, etc.) line indentation to match first line in a list with custom numbering using CSS? 使用选择器来获取子元素? - Use a selector to get a child element? 我可以在量角器中使用&#39;&gt;&#39;CSS选择器吗? - can i use '>' css selector in protractor? 我可以在jQuery中使用$(this)和CSS选择器吗? - Can I use $(this) along with a css selector in jQuery? 如何在jquery中使用fadeTo等动画标签和css更改语句? - How to use fadeTo, etc. animation tags along with a css change statement in jquery?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM