【问题标题】:Explicit wait not working NODEJS Selenium显式等待不起作用 NODEJS Selenium
【发布时间】:2021-08-16 18:48:03
【问题描述】:

这是我的代码:

const { Builder, By, Key, until, WebDriver } = require("selenium-webdriver");
const firefox = require("selenium-webdriver/firefox");
const { app, BrowserWindow, Menu } = require("electron");
const path = require("path");
const electron = require("electron");


// function for implicit waits (sleep)
function sleep(ms) {
  return new Promise((resolve) => setTimeout(resolve, ms));
}

function initSelenium() {
  async function startWebapp() {
    // build firefox webdriver and load web app
    let driver = new Builder()
      .forBrowser("firefox")
      .build();

    
    await driver.get("https://www.ea.com/en-en/fifa/ultimate-team/web-app/");

    
    // if login button appears press it - otherwise idle
    
    try {
      await driver.wait(until.elementIsVisible(By.xpath("/html/body/main/div/div/div/button[1]")), 30000);
      
      await sleep(10000);
      await driver
        .findElement(By.xpath("/html/body/main/section/nav/button[3]"))
        .click();
    } catch (err) {
      await sleep(10000);
    }
  }
  startWebapp();
}

我尝试了等待/直到(elementIsVisable 等)的多种变体,但没有工作。 当我删除我的隐式等待(睡眠)时,按钮将永远不会被按下。

我错过了什么?

【问题讨论】:

    标签: javascript node.js selenium webdriver webdriverwait


    【解决方案1】:

    https://github.com/SeleniumHQ/selenium/issues/2935

    elementIsVisible 需要一个 web 元素而不是“By”定位器。所以首先使用

    找到元素
       element = driver.findElement(xpath)
       await driver.wait(until.elementIsVisible(element),5000)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-09
      • 1970-01-01
      • 2013-11-15
      • 2022-06-22
      • 2013-04-12
      • 1970-01-01
      相关资源
      最近更新 更多