Javascript selenium,等待页面完全加载
我正试图在网页上使用selenium,但是我的代码并不一致,有时什么都没有发生,Javascript selenium,等待页面完全加载,javascript,selenium,Javascript,Selenium,我正试图在网页上使用selenium,但是我的代码并不一致,有时什么都没有发生,驱动程序。等待承诺没有实现,它只是永远等待。。。我的代码如下所示: await this.driver.wait(until.elementLocated(By.xpath("xpath1"))); const searchField = await this.driver.findElement(By.xpath("xpath1")); await searchField.c
驱动程序。等待
承诺没有实现,它只是永远等待。。。我的代码如下所示:
await this.driver.wait(until.elementLocated(By.xpath("xpath1")));
const searchField = await this.driver.findElement(By.xpath("xpath1"));
await searchField.click();
const input = await this.getElement("xpath2");
await input.sendKeys("SearchTerm", Key.RETURN);
为什么它有时工作有时不工作,我知道XPath不会改变,我已经尝试过设置非常高的硬编码this.driver.sleep(10000)
超时。试试这个:
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
这是什么
Selenium中的隐式等待用于告诉web驱动程序在抛出“无此类元素异常”之前等待一定的时间。默认设置为0。一旦我们设置了时间,web驱动程序将在抛出异常之前等待元素一段时间
SeleniumWeb驱动程序借用了Watir的隐式等待思想
在下面的示例中,我们声明了时间范围为10秒的隐式等待。这意味着,如果元素在该时间范围内不在网页上,它将抛出异常
基本语法:
driver.manage().timeouts().implicitlyWait(TimeOut, TimeUnit.SECONDS);
编辑:
如果这没有任何帮助,您可以尝试以下方法:
driver.wait(function () {
return driver.isElementPresent(webdriver.By.xpath("yourxpath"));
},超时)
//我其实并不熟悉javascript,但我希望你明白我的意思
这会使webdriver等待10秒钟,直到您要查找的元素可见为止谢谢您的回答!不过,这并没有任何区别。@VictorGunnarsson我更新了我的答案检查,看这是否有效