Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/445.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Selenium webdriver JS-如何等待元素可见_Javascript_Selenium_Selenium Webdriver_Xpath_Webdriver - Fatal编程技术网

Javascript Selenium webdriver JS-如何等待元素可见

Javascript Selenium webdriver JS-如何等待元素可见,javascript,selenium,selenium-webdriver,xpath,webdriver,Javascript,Selenium,Selenium Webdriver,Xpath,Webdriver,使用(),如何等待元素可见 我有以下功能,是一组自制测试助手的一部分,第一个可以工作,但第二个失败(例如,它超时等待和元素可见,即使它存在——由第一个可以工作的功能确认——并且是可见的——由html/css/js页面的所有可想象的测试和检查确认) 这是: /** * Wait for an element to exist * * @param {object} locator * @param {int} timeout (ms) * * @return {Promise&

使用(),如何等待元素可见

我有以下功能,是一组自制测试助手的一部分,第一个可以工作,但第二个失败(例如,它超时等待和元素可见,即使它存在——由第一个可以工作的功能确认——并且是可见的——由html/css/js页面的所有可想象的测试和检查确认)

这是:

/**
 * Wait for an element to exist
 * 
 * @param  {object} locator
 * @param  {int} timeout (ms)
 * 
 * @return {Promise<element>}
 */
// !! THIS WORKS OK
exports.waitForElement = function (locator, timeout) {
  var waitTimeout = timeout || DEFAULT_TIMEOUT;

  return this.wait(until.elementLocated(locator), waitTimeout)
    .then(() => {

        return this.findElement(locator);
      });
};


/**
 * Wait for an element to exist and then wait for it to be visible
 *
 * IMPORTANT: this is probable what you want to use instead of
 *   waitForVisibleElement most of the time.
 * 
 * @param  {hash} locator
 * @param  {number} timeout
 * 
 * @return {Promise<element>}
 */
// !! THIS FAILS TO WORK AS EXPECTED
exports.waitForVisibleElement = function (locator, timeout) {
  var waitTimeout = timeout || DEFAULT_TIMEOUT;

  return this.waitForElement(locator, waitTimeout) 
    .then(el => {
      console.log('--- element found:', el);
      return this.wait(until.elementIsVisible(el), waitTimeout)

        .then(() => {
          console.log('--- element visible!');
          // this is to make sure we are returning the same kind of
          // promise as waitForElement
          return this.findElement(locator);
        });

    });
};
/**
*等待元素存在
* 
*@param{object}定位器
*@param{int}超时(毫秒)
* 
*@return{Promise}
*/
// !! 这样行吗
exports.waitForElement=函数(定位器,超时){
var waitTimeout=超时| |默认超时;
返回此.wait(直到.elementLocated(定位器),waitTimeout)
.然后(()=>{
返回此.findElement(定位器);
});
};
/**
*等待元素存在,然后等待它可见
*
*重要提示:这可能是您想要使用的,而不是
*waitForVisibleElement在大多数情况下。
* 
*@param{hash}定位器
*@param{number}超时
* 
*@return{Promise}
*/
// !! 这并没有达到预期效果
exports.waitForVisibleElement=函数(定位器,超时){
var waitTimeout=超时| |默认超时;
返回此.waitForElement(定位器,waitTimeout)
.然后(el=>{
log('---找到的元素:',el);
返回此.wait(直到.elementIsVisible(el),waitTimeout)
.然后(()=>{
log('---元素可见!');
//这是为了确保我们返回相同类型的
//作为等待要素的承诺
返回此.findElement(定位器);
});
});
};
…我在多个上下文中进行了测试,因此这不是问题的其他原因,而是
waitForVisibleElement
中的代码,但我似乎找不到任何原因说明它不起作用


作为澄清,
对于该代码,在使用
augment
方法monkeypatches对给定webdriver对象进行修补后,最终成为webdriver实例(新的webdriver.Builder().withCapabilities(webdriver.Capabilities.chrome()).build())。。。可能是一个有问题的设计模式,但这里没有问题的原因:)



更新:显然,这只会发生在XPath定位器上,如
{XPath:'/*[contains(text(),“first name”)]}
。。。现在,这并不是说它更有意义。另外,Firefox也是如此,所以它不是一个奇怪的chrome网络驱动程序……

很可能是一个承诺问题。 请尝试以下方法:

exports.waitForElement = function (locator, timeout) {
  var timeout = timeout || DEFAULT_TIMEOUT;
  return this.wait(until.elementLocated(locator), timeout);
};

exports.waitForVisibleElement = function (locator, timeout) {
  var timeout = timeout || DEFAULT_TIMEOUT;
  var element = this.wait(until.elementLocated(locator), timeout);
  return this.wait(new until.WebElementCondition('for element to be visible ' + locator, function() {
    return element.isDisplayed().then(v => v ? element : null);
  }), timeout);
};
用法:

driver.get("...");

driver.waitForElement(By.id("..."), 2000).getText().then(function(text){
  console.log(text);
});

driver.waitForVisibleElement(By.id("..."), 2000).getText().then(function(text){
  console.log(text);
});

谢谢你帮助我更好地构建我的承诺代码。但这并不是唯一的问题。最后,我发现的唯一解决办法是避免使用所有xpath选择器,并添加特殊代码,以测试浏览器内部的可见性,使用
executeScript
测试我使用xpath的情况。。。也许最好的想法就是放弃所有的希望,一次又一次的攻击,直到事情在90%以上的时间里都能正常运作。。。期望selenium/webdriver以一种可预期/可预测的方式做任何事情都是毫无意义的……我很高兴它有所帮助。尽管我从未对XPath有过任何问题。你能添加一个例子,或者更好,用一个可复制的例子创建一个新问题吗?