我使用以下代码从下面的html中获取值:
//findTables(driver);
WebElement content = driver.findElement(By.id("tblTaskForms"));
List<WebElement> elements = content.findElements(By.className("form-name name-column assets-Tag ng-binding"));
WebElement myid = content.findElement(By.xpath("//tbody/tr[2]/td[2])[2]"));
运行上面的代码时会抛出异常。如果您有任何反馈,请告诉我。感谢
<div style="margin-bottom:20px; padding-top:20px" class="dropdown ng-scope"> <button ng-click="getModule()" class="btn btn-tasks">New asset</button> <button ng-click="showImportDialog()" class="btn btn-tasks">Import assets</button> <button class="btn btn-tasks pull-right btn-search"><i class="glyphicon glyphicon-search"></i>Search</button> <input ng-change="search()" ng-model="app.search" class="form-control form-control-hl pull-right input-search ng-pristine ng-valid" /> </div>
<table ng-show="listData.assets.length" class="table table-striped table-hover ng-scope" id="tblTaskForms">
<thead>
<tr>
<th style="border:0px; width:20px">
<div class="default-title"></div>
<input type="text" style="border-left:1px #F5F5F5 solid" readonly="readonly" class="filter" />
</th>
<th style="border:0px">
<div class="default-title assets-Tag">Tag</div>
<input type="text" name="Tag" placeholder="Search" class="filter" />
</th>
<th style="border:0px; width:65px">
<div class="default-title assets-Forms">Forms</div>
<input type="text" name="Forms" placeholder="Search" class="filter" />
</th>
<th style="border:0px; width:100px">
<div class="default-title assets-Status">Status</div>
<input type="text" name="Status" placeholder="Search" class="filter" />
</th>
<!-- ngIf: assetSettings.category1Enabled -->
<th ng-if="assetSettings.category1Enabled" style="border:0px; width:130px" class="ng-scope">
<div class="default-title assets-Class ng-binding">Class</div>
<input type="text" name="Class" placeholder="Search" class="filter" />
</th>
<!-- end ngIf: assetSettings.category1Enabled --><!-- ngIf: assetSettings.category2Enabled -->
<th ng-if="assetSettings.category2Enabled" style="border:0px; width:100px" class="ng-scope">
<div class="default-title assets-Type ng-binding">Type</div>
<input type="text" name="Type" placeholder="Search" class="filter" />
</th>
<!-- end ngIf: assetSettings.category2Enabled --><!-- ngIf: assetSettings.category3Enabled -->
<th ng-if="assetSettings.category3Enabled" style="border:0px; width:100px" class="ng-scope">
<div class="default-title assets-Area ng-binding">Area</div>
<input type="text" name="Area" placeholder="Search" class="filter" />
</th>
<!-- end ngIf: assetSettings.category3Enabled --><!-- ngIf: assetSettings.category4Enabled -->
<th ng-if="assetSettings.category4Enabled" style="border:0px; width:100px" class="ng-scope">
<div class="default-title assets-Location ng-binding">Location</div>
<input type="text" name="Location" placeholder="Search" class="filter" />
</th>
<!-- end ngIf: assetSettings.category4Enabled -->
<th style="border:0px; width:20px; max-width: 20px">
<div class="default-title"></div>
<div class="input-substitute"></div>
</th>
<th style="border:0px; width:20px">
<div class="default-title"></div>
<input type="text" readonly="readonly" placeholder="Search" class="filter" />
</th>
</tr>
</thead>
<tbody id="tbodyTaskForms">
<!-- ngRepeat: el in listData.assets -->
<tr ng-show="!el.out" ng-repeat="el in listData.assets" class="ng-scope">
<td class="form-del"> <span title="Edit this item" ng-click="getModule(el.ID)" aria-hidden="true" ng-hide="pusblished" class="glyphicon glyphicon-edit trash-icon"></span> </td>
<td ng-click="getModule(el.ID)" class="form-name name-column assets-Tag ng-binding">1 Satish Assest </td>
<td class="form-name assets-Forms ng-binding">1 </td>
<td class="form-name assets-Status ng-binding">Nominal </td>
<!-- ngIf: assetSettings.category1Enabled -->
<td ng-if="assetSettings.category1Enabled" class="form-name assets-Class ng-binding ng-scope"> </td>
<!-- end ngIf: assetSettings.category1Enabled --><!-- ngIf: assetSettings.category2Enabled -->
<td ng-if="assetSettings.category2Enabled" class="form-name assets-Type ng-binding ng-scope"> </td>
<!-- end ngIf: assetSettings.category2Enabled --><!-- ngIf: assetSettings.category3Enabled -->
<td ng-if="assetSettings.category3Enabled" class="form-name assets-Area ng-binding ng-scope"> </td>
<!-- end ngIf: assetSettings.category3Enabled --><!-- ngIf: assetSettings.category4Enabled -->
<td ng-if="assetSettings.category4Enabled" class="form-name assets-Location ng-binding ng-scope"> </td>
<!-- end ngIf: assetSettings.category4Enabled -->
<td class="form-del"> <span title="Copy/Move this item" ng-click="copyDomain(el, 'Asset')" ng-hide="pusblished" class="fa fa-copy"></span> </td>
<td class="form-del"> <span title="Delete this item" ng-click="deleteDomain(el.ID, 'Asset')" aria-hidden="true" ng-hide="pusblished" class="glyphicon glyphicon-trash trash-icon"></span> </td>
</tr>
<!-- end ngRepeat: el in listData.assets -->
<tr ng-show="!el.out" ng-repeat="el in listData.assets" class="ng-scope">
<td class="form-del"> <span title="Edit this item" ng-click="getModule(el.ID)" aria-hidden="true" ng-hide="pusblished" class="glyphicon glyphicon-edit trash-icon"></span> </td>
<td ng-click="getModule(el.ID)" class="form-name name-column assets-Tag ng-binding">2424 </td>
<td class="form-name assets-Forms ng-binding">0 </td>
<td class="form-name assets-Status ng-binding">Nominal </td>
<!-- ngIf: assetSettings.category1Enabled -->
<td ng-if="assetSettings.category1Enabled" class="form-name assets-Class ng-binding ng-scope">2 </td>
<!-- end ngIf: assetSettings.category1Enabled --><!-- ngIf: assetSettings.category2Enabled -->
<td ng-if="assetSettings.category2Enabled" class="form-name assets-Type ng-binding ng-scope">2 </td>
<!-- end ngIf: assetSettings.category2Enabled --><!-- ngIf: assetSettings.category3Enabled -->
<td ng-if="assetSettings.category3Enabled" class="form-name assets-Area ng-binding ng-scope">2 </td>
<!-- end ngIf: assetSettings.category3Enabled --><!-- ngIf: assetSettings.category4Enabled -->
<td ng-if="assetSettings.category4Enabled" class="form-name assets-Location ng-binding ng-scope">2
</td>
<!-- end ngIf: assetSettings.category4Enabled -->
<td class="form-del"> <span title="Copy/Move this item" ng-click="copyDomain(el, 'Asset')" ng-hide="pusblished" class="fa fa-copy"></span> </td>
<td class="form-del"> <span title="Delete this item" ng-click="deleteDomain(el.ID, 'Asset')" aria-hidden="true" ng-hide="pusblished" class="glyphicon glyphicon-trash trash-icon"></span> </td>
</tr>
<!-- end ngRepeat: el in listData.assets -->
<tr ng-show="!el.out" ng-repeat="el in listData.assets" class="ng-scope">
<td class="form-del"> <span title="Edit this item" ng-click="getModule(el.ID)" aria-hidden="true" ng-hide="pusblished" class="glyphicon glyphicon-edit trash-icon"></span> </td>
<td ng-click="getModule(el.ID)" class="form-name name-column assets-Tag ng-binding">24242 </td>
<td class="form-name assets-Forms ng-binding">0 </td>
<td class="form-name assets-Status ng-binding">Nominal </td>
<!-- ngIf: assetSettings.category1Enabled -->
<td ng-if="assetSettings.category1Enabled" class="form-name assets-Class ng-binding ng-scope">3 </td>
<!-- end ngIf: assetSettings.category1Enabled --><!-- ngIf: assetSettings.category2Enabled -->
<td ng-if="assetSettings.category2Enabled" class="form-name assets-Type ng-binding ng-scope">3 </td>
<!-- end ngIf: assetSettings.category2Enabled --><!-- ngIf: assetSettings.category3Enabled -->
<td ng-if="assetSettings.category3Enabled" class="form-name assets-Area ng-binding ng-scope">3 </td>
<!-- end ngIf: assetSettings.category3Enabled --><!-- ngIf: assetSettings.category4Enabled -->
<td ng-if="assetSettings.category4Enabled" class="form-name assets-Location ng-binding ng-scope">3
</td>
<!-- end ngIf: assetSettings.category4Enabled -->
<td class="form-del"> <span title="Copy/Move this item" ng-click="copyDomain(el, 'Asset')" ng-hide="pusblished" class="fa fa-copy"></span> </td>
<td class="form-del"> <span title="Delete this item" ng-click="deleteDomain(el.ID, 'Asset')" aria-hidden="true" ng-hide="pusblished" class="glyphicon glyphicon-trash trash-icon"></span> </td>
</tr>
<!-- end ngRepeat: el in listData.assets -->
<tr ng-show="!el.out" ng-repeat="el in listData.assets" class="ng-scope">
<td class="form-del"> <span title="Edit this item" ng-click="getModule(el.ID)" aria-hidden="true" ng-hide="pusblished" class="glyphicon glyphicon-edit trash-icon"></span> </td>
<td ng-click="getModule(el.ID)" class="form-name name-column assets-Tag ng-binding">Fire Extinguisher 201 </td>
<td class="form-name assets-Forms ng-binding">2 </td>
<td class="form-name assets-Status ng-binding">Disabled </td>
<!-- ngIf: assetSettings.category1Enabled -->
<td ng-if="assetSettings.category1Enabled" class="form-name assets-Class ng-binding ng-scope">Safety </td>
<!-- end ngIf: assetSettings.category1Enabled --><!-- ngIf: assetSettings.category2Enabled -->
<td ng-if="assetSettings.category2Enabled" class="form-name assets-Type ng-binding ng-scope">Fire Extinguisher </td>
<!-- end ngIf: assetSettings.category2Enabled --><!-- ngIf: assetSettings.category3Enabled -->
<td ng-if="assetSettings.category3Enabled" class="form-name assets-Area ng-binding ng-scope">Main Plant </td>
<!-- end ngIf: assetSettings.category3Enabled --><!-- ngIf: assetSettings.category4Enabled -->
<td ng-if="assetSettings.category4Enabled" class="form-name assets-Location ng-binding ng-scope">North Side </td>
<!-- end ngIf: assetSettings.category4Enabled -->
<td class="form-del"> <span title="Copy/Move this item" ng-click="copyDomain(el, 'Asset')" ng-hide="pusblished" class="fa fa-copy"></span> </td>
<td class="form-del"> <span title="Delete this item" ng-click="deleteDomain(el.ID, 'Asset')" aria-hidden="true" ng-hide="pusblished" class="glyphicon glyphicon-trash trash-icon"></span> </td>
</tr>
<!-- end ngRepeat: el in listData.assets -->
<tr ng-show="!el.out" ng-repeat="el in listData.assets" class="ng-scope">
<td class="form-del"> <span title="Edit this item" ng-click="getModule(el.ID)" aria-hidden="true" ng-hide="pusblished" class="glyphicon glyphicon-edit trash-icon"></span> </td>
<td ng-click="getModule(el.ID)" class="form-name name-column assets-Tag ng-binding">New Satish </td>
<td class="form-name assets-Forms ng-binding">0 </td>
<td class="form-name assets-Status ng-binding">Nominal </td>
<!-- ngIf: assetSettings.category1Enabled -->
<td ng-if="assetSettings.category1Enabled" class="form-name assets-Class ng-binding ng-scope"> </td>
<!-- end ngIf: assetSettings.category1Enabled --><!-- ngIf: assetSettings.category2Enabled -->
<td ng-if="assetSettings.category2Enabled" class="form-name assets-Type ng-binding ng-scope"> </td>
<!-- end ngIf: assetSettings.category2Enabled --><!-- ngIf: assetSettings.category3Enabled -->
<td ng-if="assetSettings.category3Enabled" class="form-name assets-Area ng-binding ng-scope"> </td>
<!-- end ngIf: assetSettings.category3Enabled --><!-- ngIf: assetSettings.category4Enabled -->
<td ng-if="assetSettings.category4Enabled" class="form-name assets-Location ng-binding ng-scope"> </td>
<!-- end ngIf: assetSettings.category4Enabled -->
<td class="form-del"> <span title="Copy/Move this item" ng-click="copyDomain(el, 'Asset')" ng-hide="pusblished" class="fa fa-copy"></span> </td>
<td class="form-del"> <span title="Delete this item" ng-click="deleteDomain(el.ID, 'Asset')" aria-hidden="true" ng-hide="pusblished" class="glyphicon glyphicon-trash trash-icon"></span> </td>
</tr>
<!-- end ngRepeat: el in listData.assets -->
<tr ng-show="!el.out" ng-repeat="el in listData.assets" class="ng-scope">
<td class="form-del"> <span title="Edit this item" ng-click="getModule(el.ID)" aria-hidden="true" ng-hide="pusblished" class="glyphicon glyphicon-edit trash-icon"></span> </td>
<td ng-click="getModule(el.ID)" class="form-name name-column assets-Tag ng-binding">Safety Requirements </td>
<td class="form-name assets-Forms ng-binding">1 </td>
<td class="form-name assets-Status ng-binding">NonNominal </td>
<!-- ngIf: assetSettings.category1Enabled -->
<td ng-if="assetSettings.category1Enabled" class="form-name assets-Class ng-binding ng-scope">Class 1 </td>
<!-- end ngIf: assetSettings.category1Enabled --><!-- ngIf: assetSettings.category2Enabled -->
<td ng-if="assetSettings.category2Enabled" class="form-name assets-Type ng-binding ng-scope">Eye Wash Station </td>
<!-- end ngIf: assetSettings.category2Enabled --><!-- ngIf: assetSettings.category3Enabled -->
<td ng-if="assetSettings.category3Enabled" class="form-name assets-Area ng-binding ng-scope">Area 2 </td>
<!-- end ngIf: assetSettings.category3Enabled --><!-- ngIf: assetSettings.category4Enabled -->
<td ng-if="assetSettings.category4Enabled" class="form-name assets-Location ng-binding ng-scope">Location 10
</td>
<!-- end ngIf: assetSettings.category4Enabled -->
<td class="form-del"> <span title="Copy/Move this item" ng-click="copyDomain(el, 'Asset')" ng-hide="pusblished" class="fa fa-copy"></span> </td>
<td class="form-del"> <span title="Delete this item" ng-click="deleteDomain(el.ID, 'Asset')" aria-hidden="true" ng-hide="pusblished" class="glyphicon glyphicon-trash trash-icon"></span> </td>
</tr>
<!-- end ngRepeat: el in listData.assets -->
<tr ng-show="!el.out" ng-repeat="el in listData.assets" class="ng-scope">
<td class="form-del"> <span title="Edit this item" ng-click="getModule(el.ID)" aria-hidden="true" ng-hide="pusblished" class="glyphicon glyphicon-edit trash-icon"></span> </td>
<td ng-click="getModule(el.ID)" class="form-name name-column assets-Tag ng-binding">tag </td>
<td class="form-name assets-Forms ng-binding">0 </td>
<td class="form-name assets-Status ng-binding">Nominal </td>
<!-- ngIf: assetSettings.category1Enabled -->
<td ng-if="assetSettings.category1Enabled" class="form-name assets-Class ng-binding ng-scope">class </td>
<!-- end ngIf: assetSettings.category1Enabled --><!-- ngIf: assetSettings.category2Enabled -->
<td ng-if="assetSettings.category2Enabled" class="form-name assets-Type ng-binding ng-scope">type </td>
<!-- end ngIf: assetSettings.category2Enabled --><!-- ngIf: assetSettings.category3Enabled -->
<td ng-if="assetSettings.category3Enabled" class="form-name assets-Area ng-binding ng-scope">Area </td>
<!-- end ngIf: assetSettings.category3Enabled --><!-- ngIf: assetSettings.category4Enabled -->
<td ng-if="assetSettings.category4Enabled" class="form-name assets-Location ng-binding ng-scope">location
</td>
<!-- end ngIf: assetSettings.category4Enabled -->
<td class="form-del"> <span title="Copy/Move this item" ng-click="copyDomain(el, 'Asset')" ng-hide="pusblished" class="fa fa-copy"></span> </td>
<td class="form-del"> <span title="Delete this item" ng-click="deleteDomain(el.ID, 'Asset')" aria-hidden="true" ng-hide="pusblished" class="glyphicon glyphicon-trash trash-icon"></span> </td>
</tr>
<!-- end ngRepeat: el in listData.assets -->
<tr ng-show="!el.out" ng-repeat="el in listData.assets" class="ng-scope">
<td class="form-del"> <span title="Edit this item" ng-click="getModule(el.ID)" aria-hidden="true" ng-hide="pusblished" class="glyphicon glyphicon-edit trash-icon"></span> </td>
<td ng-click="getModule(el.ID)" class="form-name name-column assets-Tag ng-binding">test asset 23 </td>
<td class="form-name assets-Forms ng-binding">1 </td>
<td class="form-name assets-Status ng-binding">Nominal </td>
<!-- ngIf: assetSettings.category1Enabled -->
<td ng-if="assetSettings.category1Enabled" class="form-name assets-Class ng-binding ng-scope">Production </td>
<!-- end ngIf: assetSettings.category1Enabled --><!-- ngIf: assetSettings.category2Enabled -->
<td ng-if="assetSettings.category2Enabled" class="form-name assets-Type ng-binding ng-scope">Chromatograph </td>
<!-- end ngIf: assetSettings.category2Enabled --><!-- ngIf: assetSettings.category3Enabled -->
<td ng-if="assetSettings.category3Enabled" class="form-name assets-Area ng-binding ng-scope">Main Plant </td>
<!-- end ngIf: assetSettings.category3Enabled --><!-- ngIf: assetSettings.category4Enabled -->
<td ng-if="assetSettings.category4Enabled" class="form-name assets-Location ng-binding ng-scope">South Side </td>
<!-- end ngIf: assetSettings.category4Enabled -->
<td class="form-del"> <span title="Copy/Move this item" ng-click="copyDomain(el, 'Asset')" ng-hide="pusblished" class="fa fa-copy"></span> </td>
<td class="form-del"> <span title="Delete this item" ng-click="deleteDomain(el.ID, 'Asset')" aria-hidden="true" ng-hide="pusblished" class="glyphicon glyphicon-trash trash-icon"></span> </td>
</tr>
<!-- end ngRepeat: el in listData.assets -->
<tr ng-show="!el.out" ng-repeat="el in listData.assets" class="ng-scope">
<td class="form-del"> <span title="Edit this item" ng-click="getModule(el.ID)" aria-hidden="true" ng-hide="pusblished" class="glyphicon glyphicon-edit trash-icon"></span> </td>
<td ng-click="getModule(el.ID)" class="form-name name-column assets-Tag ng-binding">testxx </td>
<td class="form-name assets-Forms ng-binding">0 </td>
<td class="form-name assets-Status ng-binding">Nominal </td>
<!-- ngIf: assetSettings.category1Enabled -->
<td ng-if="assetSettings.category1Enabled" class="form-name assets-Class ng-binding ng-scope"> </td>
<!-- end ngIf: assetSettings.category1Enabled --><!-- ngIf: assetSettings.category2Enabled -->
<td ng-if="assetSettings.category2Enabled" class="form-name assets-Type ng-binding ng-scope"> </td>
<!-- end ngIf: assetSettings.category2Enabled --><!-- ngIf: assetSettings.category3Enabled -->
<td ng-if="assetSettings.category3Enabled" class="form-name assets-Area ng-binding ng-scope"> </td>
<!-- end ngIf: assetSettings.category3Enabled --><!-- ngIf: assetSettings.category4Enabled -->
<td ng-if="assetSettings.category4Enabled" class="form-name assets-Location ng-binding ng-scope"> </td>
<!-- end ngIf: assetSettings.category4Enabled -->
<td class="form-del"> <span title="Copy/Move this item" ng-click="copyDomain(el, 'Asset')" ng-hide="pusblished" class="fa fa-copy"></span> </td>
<td class="form-del"> <span title="Delete this item" ng-click="deleteDomain(el.ID, 'Asset')" aria-hidden="true" ng-hide="pusblished" class="glyphicon glyphicon-trash trash-icon"></span> </td>
</tr>
<!-- end ngRepeat: el in listData.assets -->
<tr ng-show="!el.out" ng-repeat="el in listData.assets" class="ng-scope">
<td class="form-del"> <span title="Edit this item" ng-click="getModule(el.ID)" aria-hidden="true" ng-hide="pusblished" class="glyphicon glyphicon-edit trash-icon"></span> </td>
<td ng-click="getModule(el.ID)" class="form-name name-column assets-Tag ng-binding">Water Tank </td>
<td class="form-name assets-Forms ng-binding">0 </td>
<td class="form-name assets-Status ng-binding">Nominal </td>
<!-- ngIf: assetSettings.category1Enabled -->
<td ng-if="assetSettings.category1Enabled" class="form-name assets-Class ng-binding ng-scope">Production </td>
<!-- end ngIf: assetSettings.category1Enabled --><!-- ngIf: assetSettings.category2Enabled -->
<td ng-if="assetSettings.category2Enabled" class="form-name assets-Type ng-binding ng-scope">Water Pump </td>
<!-- end ngIf: assetSettings.category2Enabled --><!-- ngIf: assetSettings.category3Enabled -->
<td ng-if="assetSettings.category3Enabled" class="form-name assets-Area ng-binding ng-scope">Area 10 </td>
<!-- end ngIf: assetSettings.category3Enabled --><!-- ngIf: assetSettings.category4Enabled -->
<td ng-if="assetSettings.category4Enabled" class="form-name assets-Location ng-binding ng-scope">Location 2
</td>
<!-- end ngIf: assetSettings.category4Enabled -->
<td class="form-del"> <span title="Copy/Move this item" ng-click="copyDomain(el, 'Asset')" ng-hide="pusblished" class="fa fa-copy"></span> </td>
<td class="form-del"> <span title="Delete this item" ng-click="deleteDomain(el.ID, 'Asset')" aria-hidden="true" ng-hide="pusblished" class="glyphicon glyphicon-trash trash-icon"></span> </td>
</tr>
<!-- end ngRepeat: el in listData.assets -->
</tbody>
</table>
答案 0 :(得分:0)
By.className()
收到一个类作为输入。在List<WebElement> elements = content.findElements(By.className("form-name name-column assets-Tag ng-binding"));
你试图发送四个。你可以使用
List<WebElement> elements = content.findElements(By.className("form-name"));
// or
List<WebElement> elements = content.findElements(By.className("name-column"));
// or
List<WebElement> elements = content.findElements(By.className("assets-Tag"));
// or
List<WebElement> elements = content.findElements(By.className("ng-binding"));
如果要同时使用所有四个,请在每个类名前使用带有点的cssSelector
(cssSelector
中的类表示)
List<WebElement> elements = content.findElements(By.cssSelector(".form-name.name-column.assets-Tag.ng-binding"));
另一个问题是WebElement myid = content.findElement(By.xpath("//tbody/tr[2]/td[2])[2]"));
,xpath无效。尝试
WebElement myid = content.findElement(By.xpath("//tbody/tr[2]/td[2]"));
答案 1 :(得分:0)
我看到两个问题
First :
//with this line
content.findElements(By.className("form-name name-column assets-Tag ng-binding"));
// The class name should not have spaces so either use css selector or xpath
List<WebElement> elements = content.findElements(By.xpath("//*[@class='form-name name-column assets-Tag ng-binding']"));
第二
content.findElement(By.xpath("//tbody/tr[2]/td[2])[2]")); // this xpath is not valid