【发布时间】: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