gpt4 book ai didi

java - 在 selenium/java 中应用过滤器后检查元素不存在

转载 作者:行者123 更新时间:2023-12-02 11:06:43 25 4
gpt4 key购买 nike

我必须验证结果是否与我应用的过滤器匹配。

我选择的过滤器是“打开”,并确保结果(这是一个列表)仅包含“打开”。

我想过使用文本“Open”的 xpath,但是每个显示结果的 xpath 都会发生如下变化:

1st result - (//SPAN[@class='ng-binding'][text()='OPEN'][text()='OPEN'])[1]

2nd result - (//SPAN[@class='ng-binding'][text()='OPEN'][text()='OPEN'])[2]

3rd result - (//SPAN[@class='ng-binding'][text()='OPEN'][text()='OPEN'])[3]

and so on...

它看起来是这样的:

enter image description here

第一个打开结果的 HTML:

<div class="issues-list-item clearfix"><div class="issue-thumbnail"><div class="thumbnail-img gallery ng-isolate-scope" ng-gallery="" images="IssuesListItemCtrl.issue.attachments" thumbs-num="11" description="IssuesListItemCtrl.issue.description" issuenum="IssuesListItemCtrl.issue.issueNumber"><!-- ngIf: image.thumb -->
<div class="ng-overlay ng-hide" ng-show="opened">
</div>
<div class="ng-gallery-content ng-hide" unselectable="on" ng-show="opened" ng-swipe-left="nextImage()" ng-swipe-right="prevImage()">
<div class="uil-ring-css ng-hide" ng-show="loading"><div></div></div>
<div class="header-section">
<div class="header-text">
<span class="heading issue-num-heading">
<div class="header-title ng-binding">Issue no</div>
<div class="header-content ng-binding">6530</div>
</span>
<span class="heading desc-heading">
<div class="header-title ng-binding">Description</div>
<div class="header-content ng-binding">Automation Test</div>
</span>
<span class="heading photo-added-heading">
<div class="header-title ng-binding">Photos added by</div>
<div class="header-content ng-binding"> ()</div>
</span>
<span class="heading status-heading">
<div class="header-title ng-binding">Status</div>
<div class="status-icon" id="statsIcon"></div>
<div class="header-content ng-binding"></div>
</span>
<span class="heading date-heading">
<div class="header-title ng-binding">Date</div>
<div class="header-content ng-binding"></div>
</span>
<span class="close-heading" title="Close">
<div class="close-btn" ng-click="closeGallery()"></div>
</span>
</div>
</div>
<a href="" target="_blank" ng-show="showImageDownloadButton()" class="download-image ng-hide"><i class="fa fa-download"></i></a>
<a class="close-popup" ng-click="closeGallery()"><i class="fa fa-close"></i></a>
<div class="nav-left previous-btn" title="Previous photo" ng-click="prevImage()"></div>
<div class="current-image" ng-click="nextImage()" ng-show="!loading" ng-mousemove="showZoomControls($event)">
<!-- hack to save the image -->
<img class="current-image-save">
</div>
<div class="nav-right next-btn" title="Next photo" ng-click="nextImage()"></div>
<span id="zoom-image" ng-mouseover="alwaysShowZoomControls()">
<div>
<!-- ngIf: !imageZoomed --><span class="zoom-out-grey ng-scope" ng-if="!imageZoomed"></span><!-- end ngIf: !imageZoomed -->
<!-- ngIf: imageZoomed -->
<span class="zoom-in" title="Zoom in" ng-click="zoomIn()"></span>
</div>
</span>
<span class="info-text">
<div><span class="photo-count ng-binding">1/0</span></div>
</span>
<div class="thumb-right-btn" ng-show="showThumbnails" ng-click="scrollRight(200)"></div>
<div class="ng-thumbnails-wrapper">
<div class="thumbnail-hide-icon" ng-show="showThumbnails" ng-click="hideThumbnailList()"></div>
<div class="thumbnail-hide-header ng-binding" ng-click="hideThumbnailList()" ng-show="showThumbnails">
Hide Thumbnails
</div>
<div class="thumbnail-show-icon ng-hide" ng-show="!showThumbnails" ng-click="showThumbnailList()"></div>
<div class="thumbnail-show-header ng-binding ng-hide" ng-click="showThumbnailList()" ng-show="!showThumbnails">
Show Thumbnails
</div>
<div class="ng-thumbnails slide-left" ng-show="showThumbnails">
<!-- ngRepeat: i in images -->
</div>
</div>
<div class="thumb-left-btn" ng-show="showThumbnails" ng-click="scrollLeft(200)"></div>
</div></div><!-- ngIf: IssuesListItemCtrl.issue.attachments.length === 0 --><div class="ng-thumb no-thumbnail ng-scope" ng-if="IssuesListItemCtrl.issue.attachments.length === 0"></div><!-- end ngIf: IssuesListItemCtrl.issue.attachments.length === 0 --></div><div class="issue-status-group"><div class="issue-status open" ng-class="IssuesListItemCtrl.issue.status | lowercase"><div class="status-icon"></div><span class="ng-binding">OPEN</span></div></div><div class="content"><div class="issue-number-n-type line"><span class="issue-number ng-binding">6530</span><span>&nbsp;</span><span class="issue-type ng-binding">DEFECT</span></div><div class="description line"><span class="ng-binding">Automation Test</span></div><!-- ngIf: IssuesListItemCtrl.getAssignedTo() --><!-- ngIf: IssuesListItemCtrl.issue.dueDate --></div><div fm-photo-slide-show="" image-list="IssuesListItemCtrl.photoSlides" class-names="issue-list-photo-slide-show"></div></div>

是否有更好的方法来断言仅显示“打开”结果?顺便说一句,结果中有一个滚动。

最佳答案

您可以使用 java 8 Streams API 来过滤 OPEN 状态:

boolean issueOtherThanOPENIsPresent = driver.findElements(By.xpath("//div[@class='issue-status-group']//span")).stream().filter(element -> !element.getAttribute("innerText").trim().equals("OPEN")).findFirst().isPresent();
if (issueOtherThanOPENIsPresent) {
System.out.println("There are issues present which contains status other than OPEN");
} else {
System.out.println("All issues contains status OPEN");
}

这里检查是否存在不包含状态“OPEN”的 WebElement,如果存在则返回“true”,否则返回“false”。此外,您不必每次都计算将出现的结果数。它将根据任何给定的文本过滤列表。

我希望它也能简化答案。

关于java - 在 selenium/java 中应用过滤器后检查元素不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50906477/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com