我有一张桌子,下面是我正在编写 selenium 测试的html行的说明:
<mat-row _ngcontent-kgo-c23="" class="mat-row ng-star-inserted" role="row">
<!---->
<mat-cell _ngcontent-kgo-c23="" class="mat-cell cdk-column-id mat-column-id ng-star-inserted" role="gridcell">
<a _ngcontent-kgo-c23="" ng-reflect-router-link="/dir,208" href="/claim/208">208</a>
</mat-cell>
<mat-cell _ngcontent-kgo-c23="" class="mat-cell cdk-column-firstName mat-column-firstName ng-star-inserted" role="gridcell">
George
</mat-cell>
</mat-row>
这是测试:
List<WebElement> idRows = super.wait(WAIT, INTERVAL).until(driver -> driver.findElements(By.className("mat-column-id")));
webDriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
//Validate only one row exist in the search result
assertEquals(1,idRows.size());
//The result has is "208" and name "George"
WebElement firstNameInSearchResult = webDriver.findElement(By.className("cdk-column-firstName"));
System.out.println("===========>" + firstNameInSearchResult.gettext()+", " + idRows.get(0));
assertTrue(firstNameInSearchResult.getText().contains("George"));
assertTrue(idRows.get(0).getText().contains(searchId));
System.out.println显示空文本。并且在该打印语句之后的断言失败。我是否以正确的方式进行文本验证?
元素是Angular元素。因此,要提取文本208和George,你必须为引入WebDriverWait,visibilityOfElementLocated()
并且可以使用以下定位策略之一:
使用xpath和getAttribute("innerHTML")
:
System.out.println(new WebDriverWait(webDriver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//mat-cell[@class='mat-cell cdk-column-id mat-column-id ng-star-inserted']/a[contains(@ng-reflect-router-link, 'dir') and contains(@href, 'claim')]"))).getAttribute("innerHTML"));
使用xpath和getText()
:
System.out.println(new WebDriverWait(webDriver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//mat-cell[@class='mat-cell cdk-column-firstName mat-column-firstName ng-star-inserted']"))).getText());
@Patty Glad能够为您提供帮助。给予好评,如果此答案/任何答案是/是有帮助的,以您为未来的读者的利益。