简体   繁体   中英

getText() in WebDriver using Java

The following is the HTML code:

<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>

I tried to get the text "This field must not be empty" as follows by using WebDriver with Java:

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

But, no text was retrieved. How can I do it? What's the wrong with my code?

Yes, there is a form. HTMl code including form as belows:

<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>

You can try as:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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