selenium等待错误的JavaScript

我正在提交一个包含selenium和幻灯片的表单,然后通过search一个表示表单已被提交的br元素来回到上一页。 有时超时将超过30s的分配时间,并会发生错误: TimeoutError: Waiting for element to be located By(xpath,//div[@id='ContactFormBody']/div/br) Wait timed out after 30003ms

我需要处理这个错误,以便我的程序不会崩溃,如果发生这种情况。 另外如果有人想提一下selenium node.js的好文档,那就太棒了!

 // code generating wait error form.then(function(self){ driver.wait(until.elementLocated(By.xpath("//div[@id='ContactFormBody']/div/br")), 30000) .then(function(){ driver.navigate().back(); }); }); // attempt at handling error form.then(function(self){ try{ driver.wait(until.elementLocated(By.xpath("//div[@id='ContactFormBody']/div/br")), 30000) .then(function(){ driver.navigate().back(); }); } catch(e){ console.log("error occurred, script will continue.") } }); 

你可以指定一个错误callback函数作为then()第二个参数

 driver.wait(until.elementLocated(By.xpath("//div[@id='ContactFormBody']/div/br")), 30000) .then(function() { driver.navigate().back(); }, function (error) { console.log("Error happened!"); console.log(error); }); }); 

这个文档页面很好地概述了在WebDriverJS中使用promise:

  • 模块selenium-webdriver / lib / promise