因此,到目前为止,我的浏览器自动化工作已按预期进行。它基本上是通过几个for
循环来测试数百(或数千)个设置组合。
但是在大多数运行中,我的一个功能在等待elementLocated
条件时陷入困境。 getElementText()
函数使用xpath定位符返回给定元素的文本。如果找不到给定元素,则它会返回“无数据”。因为经常刷新DOM,所以我不得不解决标准的findElement()
方法。该函数如下:
async function getElementText(xPathStringOfElement){
await console.log ('starting getElementText() ...');
let textOfElement = 'NO DATA';
await driver.wait(await until.elementLocated(By.xpath(xPathStringOfElement), 2000)).getText()
.then(async function (text0){
textOfElement = text0;
},async function (err0){
await console.log('ERROR: ' + err0);
await console.log('...')
await console.log('Wait for Element to be shown.')
await driver.sleep(1000);
await driver.wait(await until.elementLocated(By.xpath(xPathStringOfElement), 2000)).getText()
.then(async function (text1){
textOfElement = text1;
},async function (err1){
await console.log('ERROR: ' + err1);
});
});
return textOfElement;
}
因此,我期望它执行的操作(以及连续执行数百次的操作)是,如果await driver.wait(await until.elementLocated(By.xpath(xPathStringOfElement), 2000)).getText()
等待2秒,该函数将跳转到async function (err0){
并出错。但是有时,我不明白为什么,getElementText()
被卡住并永远等待。
有人知道我如何在这里改善代码吗?
谢谢!
答案 0 :(得分:0)
添加另一个await
并使用之后:
await driver.wait(await until.elementLocated(await By.xpath(xPathStringOfElement), 2000)).getText()
现在看来运行良好。