我有以下由Angular CLI生成的HTML(我是Selenium和Angular的新手):
<ul class="projects">
<li *ngFor="let project of projects">
<a routerLink="/detail/{{project.id}}">
<span class="badge">{{project.id}}</span> {{project.projectName}}
</a>
<button class="delete" title="delete project"
(click)="delete(project)">x</button>
</li>
</ul>
我正在尝试检索由结束范围之后的字符串project.projectName表示的文本。到目前为止,我已经尝试过:
String foundProjectName = driver.findElement(By.xpath("//
[@class='projects']/li")).getText();
返回project.id,project.projectName,和为“x”。另外,
String foundProjectName = driver.findElement(By.cssSelector("span.badge +
span")).getText();
返回project.id。
答案 0 :(得分:2)
您可以获取包含ID和名称的外部文本,然后删除仅包含名称的ID的内部文本。
String outer = driver.findElement(By.cssSelector("ul.projects a")).getText(); // project.id project.projectName
String inner = driver.findElement(By.cssSelector("span.badge")).getText(); // project.id
String projectName = outer.replace(inner, "").trim();
System.out.println(projectName);