繁体   English   中英

不可靠的点击项目Selenium WebdriverJS

[英]Unreliable click item Selenium WebdriverJS

我正在尝试从React.js Web应用程序中单击动态加载的项目。 该项打开一个类名为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();
      });

大约五分之一,这会抛出'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!");
      });

这看起来效果更好,但仍然会引发大约十分之一。 在网页上, Actions似乎有效,因为项目在hover显示,但从未点击过。

我想你可以试试JavaScript执行器......就像

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

你的第一个问题是你没有正确地束缚你的承诺。 如果你压扁你的承诺,你会更容易看到问题:

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

也就是说,在发送click事件和浏览器响应之间仍然存在一些延迟。 例如,您可能需要添加等待新元素变为可见的内容。

暂无
暂无

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

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