来自以下HTML代码
<div class="message-main-contents">
<div class="message-title">Title Message</div>
<div class="message-content">
<div class="licenseexpiryannouncement">
<div>
Message Content 1
<a class="licenseexpiryannouncement-installlicenselink">Link that can be clicked</a>
under Home> Upload.
</div>
<div>Message Content 2</div>
</div>
</div>
我正在尝试使用以下代码检索“标题消息”文本,但返回空字符串
@FindBy(how = How.XPATH, using = "//div[contains(@class, 'message-title')]")
private WebElement contentMessageTitle;
String titleMessage = contentMessageTitle.getText();
System.out.println(titleMessage);
答案 0 :(得分:0)
以下是您的问题的答案:
@FindBy(how = How.XPATH, using = "//div[@class='message-title']")
private WebElement contentMessageTitle;
String titleMessage = contentMessageTitle.getText();
System.out.println(titleMessage);
或
@FindBy(how = How.XPATH, using = "//div[@class='message-main-contents']/div[@class='message-title']")
private WebElement contentMessageTitle;
String titleMessage = contentMessageTitle.getText();
System.out.println(titleMessage);
或
@FindBy(how = How.XPATH, using = "//div[@class='message-main-contents']/div[@class='message-title']")
private WebElement contentMessageTitle;
String titleMessage = contentMessageTitle.getAttribute("text");
System.out.println(titleMessage);
如果这回答你的问题,请告诉我。
答案 1 :(得分:0)
我有时在我的代码中遇到过这样的问题:getText()方法不会返回文本,而是返回一个空字符串。我通过阅读属性值&#34; textContent&#34; 找到了解决方法。
您可以使用变通方法
来阅读文本String titleMessage = contentMessageTitle.getAttribute("textContent");
通过这种方式,您可以阅读&#39; contentMessageTitle&#39; 定位器的内容。