javascript - 不同的元素正在接收点击,即使 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 次
4 次测试按预期运行。当我尝试单击属性元素时,属性元素正在接收点击。
4 次测试没有按预期运行。当我尝试单击属性元素时,修复元素正在接收单击,即使我什至没有编写任何代码来单击修复元素。
2 次没有元素收到点击。
没有焦点问题,因为在单击代码中,我在单击之前移动到元素。我使用的 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);
}
}
推荐阅读
- ios - 如何使用 alamofire 将数组数据发送到 api?
- javascript - 如何将这两个功能合二为一?
- java - 如何在 Liquibase 中使用其他表中的列创建索引?
- apache-spark - spark结构化流检查点目录下的子目录
- javascript - 以下哪项决定是否在浏览器中启用 cookie?
- reactjs - TypeError: (node.text || []).map 不是函数
- android - 在Android项目中,单元测试能达到的合理代码覆盖率是多少?
- python - 为什么最后一个函数没有执行?
- php - PHP 7.3 使用 PDO SQLSRV 返回奇怪的结果
- html - 如何在表单中定位 div 类?