Javascript 不可靠的单击项Selenium WebdriverJS

Javascript 不可靠的单击项Selenium WebdriverJS,javascript,selenium-webdriver,promise,selenium-chromedriver,Javascript,Selenium Webdriver,Promise,Selenium Chromedriver,我正在尝试单击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 tim

我正在尝试单击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();
      });
大约5次尝试中有一次,这会抛出
“无法在指定的时间内找到模态元素”
,因为模态没有实际打开

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

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!");
      });

这似乎效果更好,但仍有1/10的投掷次数。在网页上,
操作
似乎起作用,因为项目显示在
悬停
上,但它从未被点击过。

我认为您可以尝试使用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();
  });

也就是说,在发送单击事件和浏览器做出反应之间仍然存在一定的延迟。例如,您可能需要添加等待新元素变为可见