C# Protractor - 在另一个元素中查找元素
C# Protractor - Finding Element Within Another Element
我正在用 HTML 测试一个 AngularJS 应用程序,看起来像这样:
<div class="ng-scope" ng-repeat="something in section.questions">
...
</div>
<div class="ng-scope" ng-repeat="something in section.questions">
...
</div>
<div class="ng-scope" ng-repeat="something in section.questions">
<div ng-switch="something.visible">
<div id="Ins_Othersumarea" class="content-detail-wrap ng-scope ng-isolate-scope" ng-switch-when="true" highlight="something.code == global.highlightedQuestionCode" highlightedcode="global.highlightedQuestionCode" ng-dblclick="logObject(something)">
<div ng-class="{divider: showDivider(section, $index)}">
<div ng-switch="something.type" class="content-detail-section">
<div class="ng-scope" ng-switch-when="TRIGGER_YES">
<ng-include class="ng-scope" src="getFragment('yes-no-fragment.html')" ng-if="!global.interview.isGroup" ng-init="baseQuestion = something">
<div class="row ng-scope">
<div class="col-sm-6" ng-switch="baseQuestion.answerValue == undefined && !global.readonly">
<div class="content-detail-right ng-scope" ng-switch-when="true">
<div class="row">
<div class="col-xs-6">
<button type="button" id="yes_Ins_Other sum area" class="btn-base-question-triggers ng-binding" ng-click="answerbaseQuestion(baseQuestion, true)" tabindex="1" setfocus="Ins_Other sum area">Yes</button>
</div>
<div class="col-xs-6">
<button type="button" id="no_Ins_Other sum area" class="btn-base-question-triggers ng-binding" ng-click="answerbaseQuestion(baseQuestion, false)" tabindex="1">No </button>
</div>
</div>
</div>
</div>
</div>
...
我正在尝试根据中继器 Div 文本内容查找嵌套的 Yes/No 按钮。
我可以通过 -
找到正确的 ng-repeat 元素
var repeatElement = NgDriver.FindElements(NgBy.Repeater("something in section.questions")).First(x => x.Text.Contains("Some Text"));
但是我想不出一种方法来抓住适当的 Yes/No 按钮..
我试过了 -
repeatElement.FindElement(By.XPath("//button[contains(.,'Yes')]"))
但是 returns 页面上 的第一个 "Yes" 按钮 (而不是像我期望的那样在 ng-repeat Div 内).
- 为什么?
- 我怎样才能完成我想做的事情?
repeatElement.FindElement(By.XPath("//button[contains(.,'Yes')]"))
这几乎是正确的,除了 XPath 表达式 必须以点开头 是上下文/repeatElement
特定的:
repeatElement.FindElement(By.XPath(".//button[contains(.,'Yes')]"))
var yesButton = repeatElement.FindElement(By.Id("yes_Ins_Other"));
或
var yesButton = repeatElement.FindElements(By.Tag("button")).FirstOrDefault(x => x.GetAttribute("id") == "yes_Ins_Other");
我正在用 HTML 测试一个 AngularJS 应用程序,看起来像这样:
<div class="ng-scope" ng-repeat="something in section.questions">
...
</div>
<div class="ng-scope" ng-repeat="something in section.questions">
...
</div>
<div class="ng-scope" ng-repeat="something in section.questions">
<div ng-switch="something.visible">
<div id="Ins_Othersumarea" class="content-detail-wrap ng-scope ng-isolate-scope" ng-switch-when="true" highlight="something.code == global.highlightedQuestionCode" highlightedcode="global.highlightedQuestionCode" ng-dblclick="logObject(something)">
<div ng-class="{divider: showDivider(section, $index)}">
<div ng-switch="something.type" class="content-detail-section">
<div class="ng-scope" ng-switch-when="TRIGGER_YES">
<ng-include class="ng-scope" src="getFragment('yes-no-fragment.html')" ng-if="!global.interview.isGroup" ng-init="baseQuestion = something">
<div class="row ng-scope">
<div class="col-sm-6" ng-switch="baseQuestion.answerValue == undefined && !global.readonly">
<div class="content-detail-right ng-scope" ng-switch-when="true">
<div class="row">
<div class="col-xs-6">
<button type="button" id="yes_Ins_Other sum area" class="btn-base-question-triggers ng-binding" ng-click="answerbaseQuestion(baseQuestion, true)" tabindex="1" setfocus="Ins_Other sum area">Yes</button>
</div>
<div class="col-xs-6">
<button type="button" id="no_Ins_Other sum area" class="btn-base-question-triggers ng-binding" ng-click="answerbaseQuestion(baseQuestion, false)" tabindex="1">No </button>
</div>
</div>
</div>
</div>
</div>
...
我正在尝试根据中继器 Div 文本内容查找嵌套的 Yes/No 按钮。
我可以通过 -
找到正确的 ng-repeat 元素var repeatElement = NgDriver.FindElements(NgBy.Repeater("something in section.questions")).First(x => x.Text.Contains("Some Text"));
但是我想不出一种方法来抓住适当的 Yes/No 按钮..
我试过了 -
repeatElement.FindElement(By.XPath("//button[contains(.,'Yes')]"))
但是 returns 页面上 的第一个 "Yes" 按钮 (而不是像我期望的那样在 ng-repeat Div 内).
- 为什么?
- 我怎样才能完成我想做的事情?
repeatElement.FindElement(By.XPath("//button[contains(.,'Yes')]"))
这几乎是正确的,除了 XPath 表达式 必须以点开头 是上下文/repeatElement
特定的:
repeatElement.FindElement(By.XPath(".//button[contains(.,'Yes')]"))
var yesButton = repeatElement.FindElement(By.Id("yes_Ins_Other"));
或
var yesButton = repeatElement.FindElements(By.Tag("button")).FirstOrDefault(x => x.GetAttribute("id") == "yes_Ins_Other");