NoSuchElementError 当元素实际存在时
NoSuchElementError when element is actually there
我的一个 angular 测试中有这一行:
element(by.id('male')).click();
测试失败并显示此消息:
NoSuchElementError: No element found using locator: By.id("male")
但是当我将 browser.sleep()
添加到测试中时,我可以看到页面上有 #male
元素。
所以我尝试添加显式等待但没有帮助。有什么问题吗?
我经常使用以下辅助函数来等待元素出现或显示。也许试一试,Protractor 会阻塞直到 return true
。也许给他们一个机会。
module.exports = {
/**
* Block until an element is displayed on the page.
*
* @param {object} elem
* The element to watch for.
*
* @param {number} [customTimeout=10000]
* How long to wait for `elem` to appear.
*/
waitForElementToBeDisplayed: function (elem, customTimeout) {
var timeout = customTimeout || 10000;
browser.wait(function () {
return elem.isDisplayed().then(
function (visible) {
return visible;
},
function () {
return false;
}
);
}, timeout, 'failed to find element ' + elem.locator());
},
/**
* Block until an element is present on the page.
*
* @param {Object} elem
* The element to watch for.
*
* @param {Number} [customTimeout=10000]
* How long to wait for 'elem' to appear.
*/
waitForElementToBePresent: function (elem, customTimeout) {
var timeout = customTimeout || 10000;
browser.wait(function () {
return elem.isPresent().then(
function (visible) {
return visible;
},
function () {
return false;
}
);
}, timeout, 'Element not present: ' + elem.locator());
}
}
我的一个 angular 测试中有这一行:
element(by.id('male')).click();
测试失败并显示此消息:
NoSuchElementError: No element found using locator: By.id("male")
但是当我将 browser.sleep()
添加到测试中时,我可以看到页面上有 #male
元素。
所以我尝试添加显式等待但没有帮助。有什么问题吗?
我经常使用以下辅助函数来等待元素出现或显示。也许试一试,Protractor 会阻塞直到 return true
。也许给他们一个机会。
module.exports = {
/**
* Block until an element is displayed on the page.
*
* @param {object} elem
* The element to watch for.
*
* @param {number} [customTimeout=10000]
* How long to wait for `elem` to appear.
*/
waitForElementToBeDisplayed: function (elem, customTimeout) {
var timeout = customTimeout || 10000;
browser.wait(function () {
return elem.isDisplayed().then(
function (visible) {
return visible;
},
function () {
return false;
}
);
}, timeout, 'failed to find element ' + elem.locator());
},
/**
* Block until an element is present on the page.
*
* @param {Object} elem
* The element to watch for.
*
* @param {Number} [customTimeout=10000]
* How long to wait for 'elem' to appear.
*/
waitForElementToBePresent: function (elem, customTimeout) {
var timeout = customTimeout || 10000;
browser.wait(function () {
return elem.isPresent().then(
function (visible) {
return visible;
},
function () {
return false;
}
);
}, timeout, 'Element not present: ' + elem.locator());
}
}