所以现在我的浏览器自动化基本上按预期工作了。它基本上是通过几个 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()
会卡住并永远等待。
有人知道如何改进我的代码吗?
谢谢!
最佳答案
添加另一个 await
并使用后:
等待 driver.wait(等待直到.elementLocated(await By.xpath(xPathStringOfElement), 2000)).getText()
现在似乎运行良好。
关于javascript - Selenium + JS : driver. wait(until.elementLocated(By(), 2000)) 有时会卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51097710/