简体   繁体   English

如何使用 Selenium WebDriver 和 Java 获取元素的内部反应文本?

[英]How do I get the inner react-text of an element using Selenium WebDriver and Java?

I am testing an application which contains a lot of dynamic data.我正在测试一个包含大量动态数据的应用程序。 I am trying to access the inner text of the last element within a group of messages.我正在尝试访问一组消息中最后一个元素的内部文本。 The HTML is: HTML 是:

<div class="MessageList">
   <li>
      <div class="Message-mine">
          <div class=Message-block">
          <div>
              <div class="Message-text">
              <!-- react-text:1200 -->
              Hey there!
              <!-- /react-text -->
          </div>
          ….
          </div>
       </div>
   </li>
   <li>
      <div class="Message-mine">
          <div class=Message-block">
          <div>
              <div class="Message-text">
              <!-- react-text:1201 -->
              Hey there again!
              <!-- /react-text -->
          </div>
          ….
          </div>
       </div>
   </li>
   ….
</div>

I would like to verify that the last 'Message-mine' block contains the text "Hey there again.".我想验证最后一个“Message-mine”块是否包含文本“Hey there again.”。 I am using Selenium Webdriver and Java.我正在使用 Selenium Webdriver 和 Java。

I have tried the following:我尝试了以下方法:

In my Page Object(Messages):在我的页面对象(消息)中:

@FindBy(xpath="//div[@class='Message-text']")
public WebElement messageTextBox;

In my test cases:在我的测试用例中:

AssertEquals(messages.messageTextBox.getText(), "Hey there!");

(I ultimately want to be able to retrieve the last message. But above, I simply wanted to verify the first.) (我最终希望能够检索到最后一条消息。但在上面,我只是想验证第一条消息。)

The rule is, if the following:规则是,如果出现以下情况:

@FindBy(xpath="//div[@class='Message-text']")
public WebElement messageTextBox;

has more than one match, then return the first one .有多个匹配项,然后返回第一个 Which sounds like the opposite of what you want.这听起来与您想要的相反。

You probably want:你可能想要:

@FindBy(className="Message-text")
public List<WebElement> messageTextBoxes;

which will give you all of them.这会给你所有的。 To get the last one now, you will need:现在要获得最后一个,您将需要:

WebElement lastMessageTextBox = messageTextBoxes.get(messageTextBoxes.size() - 1);
AssertEquals(lastMessageTextBox.getText(), "Hey there!");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM