WebElement 的 getAttribute 有问题
Having trouble with getAttribute for a WebElement
我正在尝试获取元素的属性。它工作正常,但突然就不行了。不确定我是否改变了什么。
错误:
TypeError: Cannot read property 'info' of undefined
方法一:
async getElementAttribute12(webElement: WebElement, whichAttribute: string): Promise<string> {
try {
let attribute = '';
await webElement.getAttribute(whichAttribute).then(async function(attr) {
await this.info('attr: ', attr);
attribute = attr;
});
return attribute;
} catch (error) {
fail(`Get attribute error. ${error}`);
}
}
方法二:
检查元素是否存在后。我认为这将是一个更好的方法。欢迎提供意见。
async getElementAttribute(webElement: WebElement, whichAttribute: string): Promise<string> {
try {
let attribute = '';
if (await browser.isElementPresent(webElement)) {
await webElement.getAttribute(whichAttribute).then(async function(attr) {
await this.info('attr: ', attr);
attribute = attr;
});
return attribute;
}
} catch (error) {
fail(`Get attribute error. ${error}`);
}
}
这里的问题是你的this
指的是错误的东西,你需要在这里使用箭头函数:
await webElement.getAttribute(whichAttribute).then(async (attr) => {
我正在尝试获取元素的属性。它工作正常,但突然就不行了。不确定我是否改变了什么。
错误:
TypeError: Cannot read property 'info' of undefined
方法一:
async getElementAttribute12(webElement: WebElement, whichAttribute: string): Promise<string> {
try {
let attribute = '';
await webElement.getAttribute(whichAttribute).then(async function(attr) {
await this.info('attr: ', attr);
attribute = attr;
});
return attribute;
} catch (error) {
fail(`Get attribute error. ${error}`);
}
}
方法二:
检查元素是否存在后。我认为这将是一个更好的方法。欢迎提供意见。
async getElementAttribute(webElement: WebElement, whichAttribute: string): Promise<string> {
try {
let attribute = '';
if (await browser.isElementPresent(webElement)) {
await webElement.getAttribute(whichAttribute).then(async function(attr) {
await this.info('attr: ', attr);
attribute = attr;
});
return attribute;
}
} catch (error) {
fail(`Get attribute error. ${error}`);
}
}
这里的问题是你的this
指的是错误的东西,你需要在这里使用箭头函数:
await webElement.getAttribute(whichAttribute).then(async (attr) => {