简体   繁体   English

Javascript selenium,等待页面完全加载

[英]Javascript selenium, wait until page has fully loaded

I am trying to use selenium on a webpage, however my code doesn't work consistently sometimes nothing happens, the driver.wait promise doesn't get fulfilled it just waits forever... My code looks like this:我试图在网页上使用 selenium,但是我的代码不能始终如一地工作,有时什么也没有发生, driver.wait承诺没有得到履行,它只是永远等待......我的代码如下所示:

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);

Why does it work sometimes and sometimes not, I know the xpaths doesn't change and I have tried setting really high hardcoded this.driver.sleep(10000) timeouts.为什么它有时工作有时不工作,我知道 xpaths 不会改变,我已经尝试设置非常高的硬编码this.driver.sleep(10000)超时。

try this :尝试这个 :

driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);

what is this :这是什么 :

The Implicit Wait in Selenium is used to tell the web driver to wait for a certain amount of time before it throws a "No Such Element Exception". Selenium 中的隐式等待用于告诉 Web 驱动程序在抛出“无此类元素异常”之前等待一段时间。 The default setting is 0. Once we set the time, the web driver will wait for the element for that time before throwing an exception.默认设置为 0。一旦我们设置了时间,Web 驱动程序将在抛出异常之前等待该元素的时间。

Selenium Web Driver has borrowed the idea of implicit waits from Watir. Selenium Web Driver 从 Watir 借用了隐式等待的思想。

In the below example we have declared an implicit wait with the time frame of 10 seconds.在下面的示例中,我们声明了一个时间范围为 10 秒的隐式等待。 It means that if the element is not located on the web page within that time frame, it will throw an exception.这意味着如果该元素在该时间范围内未位于网页上,则会引发异常。

Basic syntax:基本语法:

driver.manage().timeouts().implicitlyWait(TimeOut, TimeUnit.SECONDS);   

Edit: If this doesn't help at allyou can try this:编辑:如果这根本没有帮助你可以试试这个:

driver.wait(function () {
return driver.isElementPresent(webdriver.By.xpath("yourxpath"));

}, timeout); }, 暂停);

//I'm actually not that fammiliar with javascript but i hope you understood what i mean //我实际上对javascript不太熟悉,但我希望你明白我的意思

this makes the webdriver wait 10 seconds till the element you are looking for is visible这使 webdriver 等待 10 秒,直到您要查找的元素可见

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM