无法在Selenium中的div下找到输入元素

时间:2017-06-05 01:53:07

标签: c# selenium selenium-webdriver

我正在尝试在js文件中找到输入元素“mpcname”,该文件位于div和table元素下。

我为编写元素而编写的代码如下所示,但是没有检测到名为mpcName的Input元素。

Driver.FindElement(By.XPath(".//input[contains(.,'mpcName')]"));

HTML代码如下所示。

<div enable-tooltips="" class="ng-scope">
<form name="InstanceDetailForm" ng-submit="doApply()" novalidate="" class="ng-pristine ng-invalid ng-invalid-required ng-valid-unnamed ng-valid-maxlength ng-valid-not-integer">
    <table class="table-condensed" style="width: 100%;">
        <tbody><tr>
            <td></td>
        </tr>
        <tr>
            <td>
                <table style="width: 100%;">
                    <tbody><tr>
                        <td class="pull-left">
                            <h4 class="ng-binding">Controller Configuration<b ng-show="InstanceIsDirty()" class="ng-hide">&nbsp;*</b></h4>
                        </td>

                        <td class="pull-right">
                            <button id="btnApply" type="submit" class="btn btn-primary" data-toggle="tooltip" data-placement="bottom" title="Save an APC instance">
                                <span class="glyphicon glyphicon-save"></span>&nbsp;Apply
                            </button>
                            &nbsp;
                            <button id="btnExportConfig" type="button" class="btn btn-primary" ng-click="goToInstanceList()" data-toggle="tooltip" data-placement="bottom" title="Cancel save and redirect to APC instance list page.">
                                <span class="glyphicon glyphicon-remove"></span>&nbsp;Cancel
                            </button>
                        </td>
                    </tr>
                </tbody></table>
            </td>
        </tr>
        <tr>
            <td>
                <table style="width: 100%" class="table-condensed table-striped  table-bordered">
                    <tbody><tr>
                        <td>Name</td>
                        <td>
                            **<input id="mpcname" name="mpcname" ng-model="mpcname" value="" ng-required="true" is-named="" size="50" autocomplete="off" ng-disabled="!allowEditInstanceName(mpcname,state)" class="ng-pristine ng-empty ng-invalid ng-invalid-required ng-valid-unnamed ng-touched" required="required" type="text">**

2 个答案:

答案 0 :(得分:1)

使用xpath ".//input[contains(.,'mpcName')]",您实际上正在寻找带有文字<input>的{​​{1}}元素。 mpcName实际上是id(和name和ng-model)属性。你可以使用

mpcName

或者

Driver.FindElement(By.Id("mpcName"));

答案 1 :(得分:0)

以下代码可能会有所帮助

Driver.FindElement(By.XPath(&#34; .//输入[含有(@id,&#39; MPC&#39)]&#34);

Driver.FindElement(By.XPath(&#34; .//输入[含有(@class,&#39; NG-PRIS&#39)]&#34);