[英]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
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);
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.
您还可以为该功能提供数字。
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();
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.