首页 > 解决方案 > 不同的元素正在接收点击,即使 Xpath 是正确且唯一的(量角器)

问题描述

我有两个元素,如下所示

属性元素 财产

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);在单击属性元素之前放置时,它会按预期单击。不过,如果是时间问题,它应该说找不到元素。但是当定位器是唯一的时,它怎么会点击修复元素呢?

希望有人回答这个问题。

提前致谢

标签: javascripttypescriptseleniumprotractorui-automation

解决方案


你错过了几个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);
    }
}

推荐阅读