【问题标题】:Unreliable click item Selenium WebdriverJS不可靠的点击项 Selenium WebdriverJS
【发布时间】:2016-06-04 17:50:36
【问题描述】:

我正在尝试从 React.js 网络应用程序中单击动态加载的项目。该项目打开一个类名为newItemView 的模式窗口。我已经尝试了很多东西,但没有什么是可靠的。它会工作几次,然后给我一个错误。

目标是单击动态项,然后单击模态窗口中的按钮。

尝试 1:

driver.wait(until.elementLocated(By.xpath(PATH_TO_DYNAMIC_ELEMENT)), MAX_WAIT_TIME,
      'Could not locate the element within the time specified')
      .then(function() {
          driver.findElement(By.xpath(PATH_TO_DYNAMIC_ELEMENT)).click();
      }); 

driver.wait(until.elementLocated(By.xpath(PATH_TO_MODAL_BUTTON)), MAX_WAIT_TIME,
      'Could not locate the modal element within the time specified')
      .then(function() {
          driver.findElement(By.xpath(PATH_TO_MODAL_BUTTON)).click();
      });

大约有 5 次尝试,这会抛出 'Could not locate the modal element within the time specified',因为模态实际上并未打开。

尝试 2 等待,然后使用Actions 移动按钮并单击:

driver.wait(until.elementLocated(By.xpath(PATH_TO_DYNAMIC_ELEMENT)), MAX_WAIT_TIME,
      'Could not locate the dynamic element within the time specified')
      .then(function() {
          driver.findElement(By.xpath(PATH_TO_DYNAMIC_ELEMENT))
                .then(function(PATH_TO_DYNAMIC_ELEMENT_BUTTON) {
                    var actions = new webdriver.ActionSequence(driver);
                    actions.mouseMove(PATH_TO_DYNAMIC_ELEMENT_BUTTON).click().perform();
                });
      });

然后检查模态是否打开

driver.findElement(webdriver.By.className("newItemView"))
      .then(function() {
        driver.findElement(By.xpath(PATH_TO_MODAL_BUTTON)).click();
      }, function (err) {
          if (err.name === "NoSuchElementError")
              console.log("Element was missing!");
      });

这似乎效果更好,但仍然会抛出大约 10 次。在网页上,Actions 似乎有效,因为该项目在 hover 上显示,但从未被点击。

【问题讨论】:

    标签: javascript selenium-webdriver promise selenium-chromedriver


    【解决方案1】:

    您的第一个问题是您没有正确地链接您的承诺。如果你扁平化你的承诺,就会更容易看到问题:

    return driver.wait(until.elementLocated(By.xpath(PATH_TO_DYNAMIC_ELEMENT)), MAX_WAIT_TIME,
      'Could not locate the dynamic element within the time specified')
      .then(function() {
          return driver.findElement(By.xpath(PATH_TO_DYNAMIC_ELEMENT));
      })
      .then(function(button) {
          var actions = new webdriver.ActionSequence(driver);
          return actions.mouseMove(button).click().perform();
      });
    

    也就是说,发送点击事件和浏览器做出反应之间仍有一些延迟。例如,您可能需要等待新元素变得可见。

    【讨论】:

    • 另外,elementLocated 应该返回元素,这消除了中间的 findElement 调用。
    【解决方案2】:

    我认为您可以尝试使用 JavaScript 执行器...比如

    WebElement YourElement= driver.findElement(By.id("YourElement-ID"));
    JavascriptExecutor ExeCutor = (JavascriptExecutor)driver;
    ExeCutor.executeScript("arguments[0].click();", YourElement);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-18
      • 1970-01-01
      • 2019-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-09
      相关资源
      最近更新 更多