I am currently working on automating the filling of a form in one of my Selenium scripts. The form consists of 4 text fields with the following HTML structure:
<input class="class" id="id" name="name" value="" autocomplete="off" wtx-context="stuff" type="text">
Once the form fields are filled out, there is a submit button as shown in the HTML code below:
<input id="id2" class="class2" value="Add Material" wtx-context="morestuff" type="submit">
Although I have successfully automated the filling and submitting of the form using Java code, I encounter a StaleElement exception after submission. To work around this issue, I navigate back to the homepage and then return to the form page. However, I am looking for a more elegant solution. Below is the Java code I am currently using:
Page.WriteToField(Page.Input_number(), "111111111");
Page.WriteToField(Page.Input_number2(), "222222222");
Page.WriteToField(Page.Input_date(), format.format(cal.getTime()));
Page.WriteToField(Page.Input_number3(), "3333");
Page.SubmitIngredient();
The WriteToField() function is defined as follows:
public void WriteToField(WebElement field, String text) {
field.click();
field.clear();
field.sendKeys(text);
}
To access all the WebElements in my page classes, I use the following format:
public WebElement Input_number() {
return wait.until(ExpectedConditions.elementToBeClickable(By.id("form1")));
}