【问题标题】:Wait for page to fully load with webdriverjs等待页面使用 webdriverjs 完全加载
【发布时间】:2019-12-10 21:23:50
【问题描述】:

使用 selenium-webdriver for javascript 等待页面完全加载的最佳方法是什么?我注意到this question 非常相似,但我需要在 javascript 中实现。

var webdriver = require('selenium-webdriver'),
    By = webdriver.By,
    until = webdriver.until;

var driver = new webdriver.Builder()
    .forBrowser('firefox')
    .build();

driver.get('http://www.google.com');

// Wait for the page to fully load here...
// Something like this...
// driver.wait(...);

// Then do other stuff here

driver.quit();

【问题讨论】:

  • 谢谢@naveenkumar。我一直在寻找 JavaScript 特定的实现,但是是的,你是对的,它与我最终使用的非常相似。

标签: javascript selenium selenium-webdriver


【解决方案1】:

我发现这可以满足我的需要。

driver.get('http://www.google.com');

driver.wait(function() {
  return driver.executeScript('return document.readyState').then(function(readyState) {
    return readyState === 'complete';
  });
});

// Do stuff after page load here

【讨论】:

  • 这正是我要发布的内容。这就是我使用的,效果很好。
【解决方案2】:
This is a solution for both the case where a click brings the user to a page in the same tab and a solution where the click opens the new page in a new browser tab


  /* waitForPageLoad
   * Get the Page ID before the click and after the click. Until the Page id from the first page
   * is no longer equal to the page id of the second page, then we know after a click of a link
   * that the new page is not yet loaded. The timeout will try this until id A is not equal to id B, or timeout.
   * @param {int} timeout
   * @param {string} link
   * @param {string} expectedTitle
   * @return {bool}
   */
  async waitForPageLoad(timeout, link, expectedTitle) {
    let oldHTMLId;
    let newHTMLId;
    let titleWaitingFor;
    const oldHtmlElement = await this.driver.wait(until.elementLocated(By.tagName('html')));
    await link.click();
    await this.driver.wait(async () => {
      const actualTitle = await this.driver.getTitle();
      titleWaitingFor = actualTitle;
      const newHtmlElement = await this.driver.wait(until.elementLocated(By.tagName('html')));
      const newHtmlElementId = await newHtmlElement.getId();
      const oldHtmlElementId = await oldHtmlElement.getId();
      oldHTMLId = oldHtmlElementId;
      newHTMLId = newHtmlElementId;
      return newHtmlElementId !== oldHtmlElementId
      && actualTitle.includes(expectedTitle);
    }, timeout);
    return oldHTMLId !== newHTMLId && titleWaitingFor.includes(expectedTitle);
  }


//This is what I use if the click opens the Page into another browser tab

  /* getWindowHandlesAndExpetedTitle
   * This function waits for the new window handle after a click instead of using a sleep.
   * Once the expected title of the new window is confirmed, the expected title and url are returned.
   * @param expeted title
   * @return string Page Title and url { title: returnTitle, url: currentUrl }.
   */
  async getWindowHandlesAndExpectedPageInfo(expectedTitle, waitTimeout = 6000) {
    try {
      await this.waitForWindowHandleCount(2);
      let returnHandles;
      let returnTitle;
      await this.driver.wait(async () => {
        const handles = await this.driver.getAllWindowHandles();
        returnHandles = handles;
        await this.driver.switchTo().window(handles[1]);
        const actualTitle = await this.driver.getTitle();
        returnTitle = actualTitle;
        return actualTitle.includes(expectedTitle);
      }, waitTimeout);
      const currentUrl = await this.driver.getCurrentUrl();
      await this.driver.close();
      await this.driver.switchTo().window(returnHandles[0]);
      return { title: returnTitle, url: currentUrl };
    } catch (err) {
      console.log(`Function: getWindowHandlesAndExpectedPageInfo failed ${err}`);
      const handles = await this.driver.getAllWindowHandles();
      await this.driver.close();
      await this.driver.switchTo().window(handles[0]);
      return null;
    }
  }

  /* waitForWindowHandleCount
   * This function is a wait for the epected number of window handles to be present.
   * @param int
   */
  async waitForWindowHandleCount(count, waitTimeout = 6000) {
    try {
      await this.driver.wait(async () => {
        const handles = await this.driver.getAllWindowHandles();
        return handles.length === count;
      }, waitTimeout);
    } catch (err) {
      console.log(`Function: waitForWindowHandleCount failed ${err} `);
    }
  }


【讨论】:

    【解决方案3】:

    ock 级 HTML 元素有一些限制:

    它们必须用空行与周围的文本隔开。 最外面的块元素的开始和结束标签不能缩进。 Markdown 不能在 HTML 块中使用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-10
      • 1970-01-01
      • 2023-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多