如何获取具有变化值的隐藏元素的文本内容

How to get the text content of hidden element with changing values

这是我要测试的元素

        <div id="console-modal" class="tab-item hide-item green-outline">
          <p id="message-box"></p>
          <button id="console-modal-button" class="white-button btn-md">close</button>
        </div>

当某些操作发生时,我正在向 <p> 添加一条消息并将 <div> 的显示设置为阻止。 在我对 selenium 的测试中,我希望得到更新的 <p> 元素中存在的消息。但它返回一个空字符串。

这是执行更新 <p> 元素的操作的代码

  it('should fill the login form and submit', () => {
    driver.findElement(By.id('username')).sendKeys('johnd');
    driver.findElement(By.id('passwd')).sendKeys('12345');
    return driver.findElement(By.id('inline-signin-button')).click();
  });

这是我为测试 #message-box 的内容而执行的测试。代替 innerHTML 我也试过 textContent。此外,当我向 <p> 元素添加一些文本时,测试能够获取内容,但在操作完成并显示框后它没有获取更新的内容。

it('should find the auth error modal and close', () => {
   driver.wait(until.elementLocated(By.id('console-modal')));
   driver.findElement(By.id('message-box')).isDisplayed();
   return expect(driver.findElement(By.id('message-box')).getAttribute('innerHTML')).to.eventually.equal('authentication fail! check your username and password')
driver.findElement(By.id('console-modal-button')).click();
 });

我希望返回的值是 <p> 元素的更新内容,但它似乎保留了该元素仍处于隐藏状态时的内容。有没有一种方法可以在显示元素时获取更新的文本内容。或者有什么方法可以延迟执行,直到元素显示后再测试它。

您可以使用下面的等到。

var pElement = driver.wait(until.elementLocated(By.xpath("//div[@id='console-modal'][not(contains(@class,'hide-'))]/p[@id='message-box']")));

我们正在等待 div class 名称没有 hide,以确保显示块(这将确保 p 文本更新)