javascript 如何在 WebDriverJS 中等待元素可点击?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26241495/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I wait for an element to be clickable in WebDriverJS?
提问by cSn
Does anybody know how to wait for a WebElement to be clickable in WebDriverJS? I already know how to wait for the element to be "visible", but I would need it to be "clickable".. Something similar to expectable conditionsin Python binding. I haven't been able to find something similar in Webdriver Js API.
有人知道如何在WebDriverJS 中等待 WebElement 可点击吗?我已经知道如何等待元素“可见”,但我需要它“可点击”。类似于Python 绑定中的预期条件。我在 Webdriver Js API 中找不到类似的东西。
采纳答案by Louis
There does not seem to be a condition equivalent to Python's selenium.webdriver.support.expected_conditions.element_to_be_clickable
. However, looking at the source for that condition, I see that it does two checks:
似乎没有与 Python 的selenium.webdriver.support.expected_conditions.element_to_be_clickable
. 但是,查看该条件的来源,我发现它进行了两项检查:
That the element is visible.
That it is enabled.
该元素是可见的。
它已启用。
So you could wait for both conditions to become true. The following code illustrates how that could be done. It first make an element invisible and disables it, sets some timeouts to make it visible and enable it, and then wait for the two conditions to happen.
所以你可以等待这两个条件都成立。以下代码说明了如何做到这一点。它首先使元素不可见并禁用它,设置一些超时以使其可见并启用它,然后等待两个条件发生。
var webdriver = require('selenium-webdriver');
var driver = new webdriver.Builder().
withCapabilities(webdriver.Capabilities.chrome()).
build();
driver.get('http://www.google.com');
// This script allows testing the wait. We make the element invisible
// and disable it and then set timeouts to make it visible and enabled.
driver.executeScript("\
var q = document.getElementsByName('q')[0];\
q.style.display = 'none';\
q.disabled = true;\
setTimeout(function () {\
q.style.display = '';\
}, 2000);\
setTimeout(function () {\
q.disabled = false;\
}, 3000);\
");
driver.findElement(webdriver.By.name('q')).then(function (element) {
driver.wait(function () {
return element.isDisplayed().then(function (displayed) {
if (!displayed)
return false;
return element.isEnabled();
});
});
element.sendKeys('webdriver');
});
driver.findElement(webdriver.By.name('btnG')).click();
driver.wait(function() {
return driver.getTitle().then(function(title) {
return title === 'webdriver - Google Search';
});
}, 1000);
driver.quit();
The code may look a bit strange due to the fact that we're working with promises. Not that promises are inherentlystrange but they take some time getting accustomed to when one is used to work with Python.
由于我们正在使用 Promise,代码可能看起来有点奇怪。并不是说 Promise本身就很奇怪,但是当人们习惯使用 Python 时,它们需要一些时间来适应。
回答by cSn
If you don't care about clicking the object once it's available** then you can do something like this:
如果您不关心在对象可用** 后单击它,那么您可以执行以下操作:
function clickWhenClickable(locator, timeout){
driver.wait(function(){
return driver.findElement(locator).then(function(element){
return element.click().then(function(){
return true;
}, function(err){
return false;
})
}, function(err){
return false;
});
}, timeout, 'Timeout waiting for ' + locator.value); ;
}
** if you justwant to check if the element is clickable without having to click itthen this snippet is not for you. In that case, I would say webdriver js does not provides the means to do that. (or at least I haven't find it yet, insights welcomed :) )
** 如果您只想检查元素是否可点击而不必点击它,那么此代码段不适合您。在那种情况下,我会说 webdriver js 没有提供这样做的方法。(或者至少我还没有找到,欢迎提供见解:))
回答by cSn
UNTIL seems to be the closest thing in webdriver js for this:
UNTIL 似乎是 webdriver js 中最接近的东西:
Check: https://seleniumhq.github.io/selenium/docs/api/javascript/module/selenium-webdriver/lib/until.html
检查:https: //seleniumhq.github.io/selenium/docs/api/javascript/module/selenium-webdriver/lib/until.html
There are already defined waiting conditions there. I don't know which one should be considered clickeable.
那里已经定义了等待条件。我不知道哪个应该被认为是可点击的。