不同的元素正在接收点击,即使 Xpath 是正确且唯一的(Protractor)

Different element is receiving the click, even though Xpath is correct and unique(Protractor)

我有如下两个元素

属性 元素

Xpath->//sprk-accordion-item[@ng-reflect-title="Property"]//sprk-icon

修复元素

Xpath->//sprk-accordion-item[@ng-reflect-title="Repair"]//sprk-icon

我用来点击元素的代码

public async click(webElement: ElementFinder) {
    try {
        this.expectConditions.waitForElementToBeVisible(webElement);
        this.moveToElement(webElement);
        await webElement.click();
    } catch (error) {
        console.log('Error while clicking the element' + webElement.locator().toString() + 'Error:: ' + error);
    }
}

    public async waitForElementToBeVisible(element: ElementFinder) {
    const ec = protractor.ExpectedConditions;
   await browser.wait(ec.visibilityOf(element), this.time, 'Unable to find the element' + element.locator().toString());
   }


public async moveToElement(webElement: ElementFinder) {
    await browser.actions().
        mouseMove(webElement).
        perform();
}


click(element(by.xpath('//sprk-accordion-item[@ng-reflect-title="Property"]//sprk-icon'));

我的问题是假设我 运行 测试了 10 次

没有焦点问题,因为在点击代码中我在点击元素之前移动到该元素。我使用的 x 路径是唯一的,它们甚至不是动态的。

当我在单击 属性 元素之前放置 browser.sleep(1000); 时,它会按预期进行单击。不过,如果是时间问题,应该说找不到元素。但是为什么当定位器是唯一的时候它会点击修复元素呢??

希望有人回答这个问题。

提前致谢

你错过了几个await:

public async click(webElement: ElementFinder) {
    try {
        await this.expectConditions.waitForElementToBeVisible(webElement);
        await this.moveToElement(webElement);
        await webElement.click();
    } catch (error) {
        console.log('Error while clicking the element' + webElement.locator().toString() + 'Error:: ' + error);
    }
}