不可靠的点击项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();
});
大约 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!");
});
这似乎效果更好,但仍然投掷了大约十分之一的次数。在网页上,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();
});
也就是说,在发送点击事件和浏览器做出反应之间仍然存在一些滞后。 例如,您可能需要添加等待新元素变得可见。
相关文章:
- Javascript:selenium Web驱动程序isDisplayed()不工作
- 模拟Selenium WebDriverJS中的按键
- 如何使用WebDriverJS捕获Selenium错误
- 不可靠的点击项Selenium WebdriverJS
- 如何使用sendkeys函数与使用Selenium webdriverjs的承诺链
- Selenium WebDriverJS from Javascript API
- 运行selenium webdriverjs测试时,如何查看浏览器日志消息
- 当使用selenium-webdriverjs运行mocha测试时,调用堆栈没有太大帮助
- 将元素拖放到特定位置-Selenium,WebDriverJS
- 如何在selenium webdriverjs中每两分钟安排一个任务?
- 不能在Selenium中启动带有WebDriverJs的ie浏览器
- Selenium Webdriverjs使用循环查找文本字符串
- 在Selenium WebDriverJS中获取WebElement的HTML源文件
- 使用selenium-webdriver包为WebDriverJS编写端到端测试
- 在Node上使用Selenium's WebDriverJs进行截图的正确语法
- 在FireFox和IE中使用Selenium WebdriverJS的问题
- WebdriverJS是原始Selenium webdriver的全功能替代品吗?
- Selenium WebDriverJS的捕获不捕获StaleElementException
- Selenium WebDriverJs无法使用iOS创建新客户端
- Selenium WebdriverJS承诺循环