Javascript 量角器 - 当子元素也是页面中其他地方的主要元素时,如何在元素内查找元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27422295/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Protractor - How to find an element inside an element when sub element is also a main element somewhere else in a page
提问by hny2015
<div class="base-view app-loaded" data-ng-class="cssClass.appState">
<div class="ng-scope" data-ng-view="">
<div class="ng-scope" data-ng-include="'partial/navigation/navigation.tpl.html'">
<div class="feedback-ball feedback-ball-show feedback-ball-big" data-ng-class="feedback.cls" data-ng-click="outside($event)" data-feedback-ball="">
<span class="close-button"></span>
<h2 class="ng-binding">Welcome to Garbo</h2>
<div class="ng-scope ng-binding" data-ng-bind-html="feedback.html" data-ng-if="feedback.html">
<p>Here you can play in style in a safe and secure environment.</p>
<p>
<a class="btn" href="/account">My Account</a>
<a class="btn" href="/deposit">Deposit</a>
</p>
</div>
</div>
</div>
I want to find and click /account button inside data-ng-bind-html="feedback.html", I can find data-ng-bind-html="feedback.html" but I could not find account button inside it. when I try to find account button, it gives me error that page has multiple account button so be more specific.
我想在 data-ng-bind-html="feedback.html" 中找到并单击 /account 按钮,我可以找到 data-ng-bind-html="feedback.html" 但我在里面找不到帐户按钮。当我尝试查找帐户按钮时,它给我错误页面有多个帐户按钮,因此请更具体。
I tried element.().element() but it didnt work, please help
我试过 element.().element() 但没有用,请帮忙
回答by alecxe
elementcalls can be chainedto find elements inside other elements, so your element().element()solution should work.
element可以链接调用以查找其他元素中的元素,因此您的element().element()解决方案应该有效。
Alternatively, you can construct an xpath expressionto reach the link inside the appropriate div:
或者,您可以构建一个xpath 表达式以到达适当的 内的链接div:
element(by.xpath('//div[@data-ng-bind-html = "feedback.html"]//a[@href = "/account"]'))
回答by Leonardo Maddio
The problem is that webDriver is finding more than one element that matches. You have element for finding just one, and element.all for taking an array of elements, then you can use .get() and the index of the element, or first() or last(). You can do,
问题是 webDriver 找到了多个匹配的元素。您可以使用 element 仅查找一个,而 element.all 用于获取元素数组,然后您可以使用 .get() 和元素的索引,或者 first() 或 last()。你可以做,
element(by.css('[data-ng-bind-html="feedback.html"]')
.element(by.cssContainingText('.btn', 'My account'));
If it doesn't work then you might have more than one, if so, you can use,
如果它不起作用,那么您可能有多个,如果是这样,您可以使用,
element(by.css('[data-ng-bind-html="feedback.html"]')
.all(by.cssContainingText('.btn', 'My account')).first();
But there you will have more than one button in your HTML, webDriver will get only one, another thing, is to use the count() that gives you the length of the array of elements, and you can know how much you have.
但是在你的 HTML 中你将有多个按钮,webDriver 只会得到一个,另一件事是使用 count() 为你提供元素数组的长度,你可以知道你有多少。

