0

I am trying to obtain the content of a element using java and htmlunit but getting some ClassCastException error. Not sure if I am getting this the correct way.

Exception:

java.lang.ClassCastException: com.gargoylesoftware.htmlunit.html.HtmlMeta cannot be cast to com.gargoylesoftware.htmlunit.html.HtmlTextArea
        at com.study.crud.web.it.htmlunit.PageIT.testCreateProductScenario(PageIT.java:34)

PageIT.java

@Test
public void testCreateProductScenario() throws IOException {
    HtmlPage homePage = webClient.getPage(BASE_URL);
    assertEquals(homePage.getWebResponse().getStatusCode(), SUCCESS_CODE); //verify home page

    //verify search
    HtmlPage createProductPage = homePage.getAnchorByText("Create Product").click();
    assertEquals(createProductPage.getWebResponse().getStatusCode(), SUCCESS_CODE); //verify create product page

    //search elements by id
    HtmlInput nameTxtBox = createProductPage.getElementByName("name");
    HtmlTextArea descTxtArea = (HtmlTextArea) createProductPage.getElementByName("description");

createProduct.jsp

  <form class="form-horizontal" role="form" name="frmCreateProduct" method="POST" action="">
                    <div class="form-group">
                        <label for="name" class="col-sm-2 control-label">Name</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" name="name" placeholder="Enter Name"/>
                        </div>
                        <br/><br/>
                        <label for="description" class="col-sm-2 control-label">Description</label>
                        <div class="col-sm-10">
                            <textarea class="form-control" name="description" rows="5" cols="100" maxlength="1000"></textarea>
                        </div>
                        <br/><br/>

Please guide.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Nital
  • 5,784
  • 26
  • 103
  • 195
  • I don't know enough about the low-level HtmlUnit API to answer as asked, but I am a big fan of [Geb](http://gebish.org) as a DSL on top of Selenium for tests like this: `$("#description").text()` – chrylis -cautiouslyoptimistic- Aug 16 '16 at 21:40
  • Are you sure there is only one element named `description`? According to the [documentation](http://htmlunit.sourceforge.net/apidocs/com/gargoylesoftware/htmlunit/html/HtmlPage.html#getElementByName(java.lang.String)), `getElementByName` returns the *first* element with that name, and according to your error, that element is a `HtmlMeta`, which suggests it's grabbing an element tagged with `meta`. – Vince Aug 16 '16 at 21:54
  • @Vince - You were absolutely right. I had another element on top of the page named `description` which was resulting in this error. I renamed my input element to something else in order to resolve the conflict and got the functionality working. Thanks a lot ! – Nital Aug 17 '16 at 03:23

0 Answers0