0

以下是 HTML 代码:

<div id="TITLE" class="text">
<label for="widget_polarisCommunityInput_113_title">Title</label>
<input type="text" name="field(TITLE)" id="widget_polarisCommunityInput_113_title">
<div class="error">This field must not be empty</div>
</div>

我尝试通过将 WebDriver 与 Java 结合使用来获取文本“此字段不能为空”,如下所示:

driver.findElement(By.xpath("//div[@id='TITLE']/div")).getText();
driver.findElement(By.xpath("//div[@class='error']")).getText();

但是,没有检索到任何文本。我该怎么做?我的代码有什么问题?

是的,有一个表格。HTML代码包括如下形式:

<form action="http://community.sandbox.no/content/save.do;jsessionid=F2BF733599D8D9F812B89ACBC20D37C5" method="post">
<div id="widget_polarisCommunityInput_113_leftcolumn">
<input type="hidden" value="article" name="articleType">
<input type="hidden" value="published" name="state">
<input type="hidden" value=" " name="successUrl">
<input type="hidden" value="" name="articleId">
<input type="hidden" value="http://sandbox.no/community/article/" name="redirect_url">
<input type="hidden" value="113" name="widget_id">
<div id="TITLE" class="text">
<label for="widget_polarisCommunityInput_113_title">Title</label>
<input type="text" name="field(TITLE)" id="widget_polarisCommunityInput_113_title">
<div class="error">This field must not be empty</div>
</div>
</form>
4

1 回答 1

0

您可以尝试如下:

driver.findElement(By.xpath("//form/div[@id='widget_polarisCommunityInput_113_leftcolumn']/div[@id='TITLE']/div")).getText();

于 2012-07-30T07:42:43.617 回答