【发布时间】:2020-09-30 18:01:31
【问题描述】:
This question 与我的标题相似,但在我的情况下,我需要找到一个基于 innerHtml 的元素,而不是找到 <T> 类型的元素并查看它的 innerHtml。
在下面的元素中,唯一标识 div 的唯一方法是基于其 innerHtml。我无法添加 id,因为那太容易了。如何在debugElement.query 语句中找到 div?
html:
<div *ngIf="isOn">Hello</div>
测试:
it('#clicked() should toggle #isOn', () => {
const comp = component;
comp.click();
fixture.detectChanges();
const div = fixture.debugElement.query(By.css('[innerHTML] === "Hello"')); // wrong
const div2 = fixture.debugElement.query(x => x.attributes['innerHtml'] === "Hello"); // also wrong
const div3 = fixture.debugElement.query(x => x.attributes['text'] === "Hello"); // also wrong
expect(div).toBeTruthy();
});
【问题讨论】:
-
看起来不需要是内部 HTML 而是文本内容...
-
By.css是量角器,不是茉莉;除了css,还有a number of methods onBy。您可能想调查这些。 -
我正在使用 Angular 我相信它是一个定义为 Angular 平台浏览器一部分的类。我也无法通过文本属性找到 div。
-
等等,你需要通过innerHtml定位元素还是找到你已经定位的元素的innerHtml
-
@SergeyPleshakov 我需要通过 innerHtml 定位一个元素