improving code to save to DB - java

I have below mentioned code in JSP Page. I am able to save all the values from below text boxes to DB. However, I read each text box name in my servlet (request.getParameter()) to save the fields. Is it possible to improve this code? like for example : to save all the values of one particular column in an array and save to db. If yes; please show an example that helps me. Also how to ignore if a row is empty. for example, user fills only two rows and rest rows are empty. I want to save only first two rows in DB.
<table class="cmn-table" id="t1" border="1" style="margin-left: 0.2em; margin- right:0em">
<col width="5%">
<col width="18%">
<col width="18%">
<col width="18%">
<col width="18%">
<col width="18%">
<tr>
<th>Sl.No</th>
<th>Source IP Address</th>
<th>Destination IP Address</th>
<th>Service and Port (TCP, UDP, etc.)</th>
<th>Start Date</th>
<th>Expiration Date</th>
</tr>
<tr>
<td style="align:center"> 1 </td>
<td><input type="text" name="name" id="name" size="20"> </td>
<td><input type="text" name="name1" id="name1" size="20"> </td>
<td><input type="text" name="name2" id="name2" size="20"> </td>
<td><input type="text" name="name3" id="name3" size="15"> <img src="../Image/cal.gif" id="" style="cursor: pointer;" onclick="javascript:NewCssCal('name3','MMddyyyy','dropdown',false,'12')" /></td>
<td><input type="text" name="name4" id="name4" size="15"><img src="../Image/cal.gif" id="" style="cursor: pointer;" onclick="javascript:NewCssCal('name4','MMddyyyy','dropdown',false,'12')" /> </td>
</tr>
<tr>
<td style="align:center"> <label> 2 </label> </td>
<td><input type="text" name="name5" id="name" size="20"> </td>
<td><input type="text" name="name6" id="name" size="20"> </td>
<td><input type="text" name="name7" id="name" size="20"> </td>
<td><input type="text" name="name8" id="name3" size="15"> <img src="../Image/cal.gif" id="" style="cursor: pointer;" onclick="javascript:NewCssCal('name3','MMddyyyy','dropdown',false,'12')" /></td>
<td><input type="text" name="name9" id="name4" size="15"><img src="../Image/cal.gif" id="" style="cursor: pointer;" onclick="javascript:NewCssCal('name4','MMddyyyy','dropdown',false,'12')" /> </td>
</tr>
<tr>
<td> <label> 3 </label> </td>
<td><input type="text" name="name10" id="name" size="20"> </td>
<td><input type="text" name="name11" id="name" size="20"> </td>
<td><input type="text" name="name12" id="name" size="20"> </td>
<td><input type="text" name="name13" id="name3" size="15"> <img src="../Image/cal.gif" id="" style="cursor: pointer;" onclick="javascript:NewCssCal('name3','MMddyyyy','dropdown',false,'12')" /></td>
<td><input type="text" name="name14" id="name4" size="15"><img src="../Image/cal.gif" id="" style="cursor: pointer;" onclick="javascript:NewCssCal('name4','MMddyyyy','dropdown',false,'12')" /> </td>
</tr>
<tr>
<td> <label>4 </label> </td>
<td><input type="text" name="name15" id="name" size="20"> </td>
<td><input type="text" name="name16" id="name" size="20"> </td>
<td><input type="text" name="name17" id="name" size="20"> </td>
<td><input type="text" name="name18" id="name3" size="15"> <img src="../Image/cal.gif" id="" style="cursor: pointer;" onclick="javascript:NewCssCal('name3','MMddyyyy','dropdown',false,'12')" /></td>
<td><input type="text" name="name19" id="name4" size="15"><img src="../Image/cal.gif" id="" style="cursor: pointer;" onclick="javascript:NewCssCal('name4','MMddyyyy','dropdown',false,'12')" /> </td>
</tr>
<tr>
<td> <label>5 </label> </td>
<td><input type="text" name="name20" id="name" size="20"> </td>
<td><input type="text" name="name21" id="name" size="20"> </td>
<td><input type="text" name="name22" id="name" size="20"> </td>
<td><input type="text" name="name23" id="name3" size="15"> <img src="../Image/cal.gif" id="" style="cursor: pointer;" onclick="javascript:NewCssCal('name3','MMddyyyy','dropdown',false,'12')" /></td>
<td><input type="text" name="name24" id="name4" size="15"><img src="../Image/cal.gif" id="" style="cursor: pointer;" onclick="javascript:NewCssCal('name4','MMddyyyy','dropdown',false,'12')" /> </td>
</tr>
</table>

As much as i understand, you want to get rid of calling request.getParameter on all the input fields.
Why dont you use below:
request.getParameterMap()
This gives you a map of all the parameter names and values. So, you can iterate through the map and keys in the map are parameter names and value for that key is/are the values for that parameter. Its a simple iteration over map. While iterating through the map, you can actually prepare your DB query as well based on other checks like null check.

Related

Finding the idNumber element from this below HTML code

<div id="rightContent" style="display: inline-block; width: 700px; vertical-align: top;">
<div>
<h1><em class="SomethingHeading">Something</em></h1>
</div>
<div class="content scaffold-list" role="main">
<h1>Search</h1>
<form action="/SomethingArchiveUI/search/search" method="post">
<table>
<tbody>
<tr>
<td>ID/Registration Number:</td>
<td>
<**input type="text" name="idNumber" maxlength="20" value="" id="idNumber"**>
</td>
</tr>
<tr>
<td>Client Name:</td>
<td>
<input type="text" name="clientName" maxlength="50" value="" id="clientName">
</td>
</tr>
<tr>
<td>Client Surname:</td>
<td>
<input type="text" name="clientSurname" maxlength="50" value="" id="clientSurname">
</td>
</tr>
<tr>
<td colspan="2">
<input type="hidden" name="_dateRange"><input type="checkbox" name="dateRange" value="true" id="dateRange"><label for="dateRange"> Use date range?</label>
</td>
</tr>
<tr>
<td colspan="2"><p>From: <input type="text" name="fromDate" value="" maxlength="10" id="fromDate" class="hasDatepicker"> To: <input type="text" name="toDate" value="" maxlength="10" id="toDate" class="hasDatepicker"></p></td>
</tr>
</tbody>
</table>
<div style="display: inline;">
<div id="divSubmit">
<input type="submit" name="cmdSearch" class="buttons" value="Search" id="cmdSearch">
</div>
<div id="divBusy" style="display: none;">
Busy Searching...
</div>
</div>
</form>
<table id="customerIdentificationList" style="display: block;">
</table>
<div id="loadingMessage" style="display: none;">
Loading. Please wait.
</div>
</div>
</div>
I used this code below for logging into the Application, but similar code doesn't seem to work on this form above. I'm trying to find the 'idNumber' element:
driver.findElement(By.xpath("//*[#id='idnumber']")).sendKeys("Username");
driver.findElement(By.xpath("//*[#id=\'password\']")).sendKeys("Password");
driver.findElement(By.xpath("//*[#id=\'submit\']")).click();
The error i get is: org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":
Looking forward to your responses.
Use : .//input[#id='idnumber']
Why not just use By.id()?
driver.findElement(By.id("idNumber"));
I don't know the reason why you can't use By.id() or By.xpath in this case. But you can try to find the input using text in previous td. I hope next code will help:
driver.findElement(By.xpath("//td[text()='ID/Registration Number:']/following-sibling::td/input")).sendKeys("Username");
Try using the below way -
First get the root element and keep it to a WebElement object. Then find for input element idNumber
WebElement elem = driver.findElement(By.cssSelector("#rightContent > div.content.scaffold-list > form"));
elem.findElement(By.id("idNumber")).sendKeys("Username");

How do I ensure that all fields are loaded on the page in selenium so that none are stale?

Problem
I have a PeopleSoft page that I am trying to test with the Selenium Web Driver for Java. However, I keep running into this error:
org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document
I have read here, here, and here, as well as other Stack Overflow questions, and none of the answers are working for me.
Layout of the Page
The PeopleSoft page that I am testing is an I9 form, and the part I care about looks like this:
The corresponding HTML is really large and messy--as all PeopleSoft pages are--and it has in the middle of it, an iframe that wraps pretty much the entire page.
For reference, this JSFiddle has all of the HTML that is inside that iframe.
Here is the portion that corresponds to the sections in the above image:
<table role='presentation' border='0' id='ACE_width' cellpadding='0' cellspacing='0' class='PSPAGECONTAINER' cols='46' width='963'>
<!-- OMITTED -->
<tr>
<td height='16'></td>
<td colspan='19' valign='top' align='left'>
<DIV id='win0divG_FORM_I9_G_LAST_NAMElbl'>
<label for='G_FORM_I9_G_LAST_NAME' id='G_FORM_I9_G_LAST_NAME_LBL' class='PSEDITBOXLABEL'>*Last Name (Family Name)</label>
</DIV>
</td>
<td colspan='9' valign='top' align='left'>
<DIV id='win0divG_FORM_I9_G_FIRST_NAMElbl'>
<label for='G_FORM_I9_G_FIRST_NAME' id='G_FORM_I9_G_FIRST_NAME_LBL' class='PSEDITBOXLABEL'>*First Name (Given Name)</label>
</DIV>
</td>
<td colspan='3' valign='top' align='left'>
<DIV id='win0divG_FORM_I9_G_MIDDLE_INITIALlbl'>
<label for='G_FORM_I9_G_MIDDLE_INITIAL' id='G_FORM_I9_G_MIDDLE_INITIAL_LBL' class='PSEDITBOXLABEL'>Middle Initial</label>
</DIV>
</td>
<td colspan='14' valign='top' align='left'>
<DIV id='win0divG_FORM_I9_G_MAIDEN_NAMElbl'>
<label for='G_FORM_I9_G_MAIDEN_NAME' id='G_FORM_I9_G_MAIDEN_NAME_LBL' class='PSEDITBOXLABEL'>Other Last Names Used (if any)</label>
</DIV>
</td>
</tr>
<tr>
<td height='32' colspan='2'></td>
<td colspan='19' nowrap='nowrap' valign='top' align='left'>
<DIV id='win0divG_FORM_I9_G_LAST_NAME'>
<input type='text' name='G_FORM_I9_G_LAST_NAME' id='G_FORM_I9_G_LAST_NAME' tabindex='43' value="Tiger" class='PSEDITBOX' style='width:232px; ' maxlength='30' onchange="addchg_win0(this);oChange_win0=this;" aria-required='true' />
</DIV>
</td>
<td colspan='9' nowrap='nowrap' valign='top' align='left'>
<DIV id='win0divG_FORM_I9_G_FIRST_NAME'>
<input type='text' name='G_FORM_I9_G_FIRST_NAME' id='G_FORM_I9_G_FIRST_NAME' tabindex='44' value="Tony" class='PSEDITBOX' style='width:160px; ' maxlength='30' onchange="addchg_win0(this);oChange_win0=this;" aria-required='true' />
</DIV>
</td>
<td colspan='2' nowrap='nowrap' valign='top' align='left'>
<DIV id='win0divG_FORM_I9_G_MIDDLE_INITIAL'>
<input type='text' name='G_FORM_I9_G_MIDDLE_INITIAL' id='G_FORM_I9_G_MIDDLE_INITIAL' tabindex='45' value="" class='PSEDITBOX' style='width:19px; ' maxlength='1' onchange="addchg_win0(this);return doEdits_win0(this,'','N','N','N','N','Y','N',0);"
/>
</DIV>
</td>
<td colspan='14' nowrap='nowrap' valign='top' align='left'>
<DIV id='win0divG_FORM_I9_G_MAIDEN_NAME'>
<input type='text' name='G_FORM_I9_G_MAIDEN_NAME' id='G_FORM_I9_G_MAIDEN_NAME' tabindex='46' value="" class='PSEDITBOX' style='width:180px; ' maxlength='50' />
</DIV>
</td>
</tr>
<tr>
<td height='16'></td>
<td colspan='21' valign='top' align='left'>
<DIV id='win0divG_FORM_I9_G_ADDRESSlbl'>
<label for='G_FORM_I9_G_ADDRESS' id='G_FORM_I9_G_ADDRESS_LBL' class='PSEDITBOXLABEL'>*Address (Street Number and Name)</label>
</DIV>
</td>
<td colspan='5' valign='top' align='left'>
<DIV id='win0divG_FORM_I9_G_APARTMENT_NBRlbl'>
<label for='G_FORM_I9_G_APARTMENT_NBR' id='G_FORM_I9_G_APARTMENT_NBR_LBL' class='PSEDITBOXLABEL'>Apt. Number</label>
</DIV>
</td>
<td colspan='7' valign='top' align='left'>
<DIV id='win0divG_FORM_I9_G_CITYlbl'>
<label for='G_FORM_I9_G_CITY' id='G_FORM_I9_G_CITY_LBL' class='PSEDITBOXLABEL'>*City or Town</label>
</DIV>
</td>
<td colspan='6' valign='top' align='left'>
<DIV id='win0divG_FORM_I9_G_STATElbl'>
<label for='G_FORM_I9_G_STATE' id='G_FORM_I9_G_STATE_LBL' class='PSEDITBOXLABEL'>*State</label>
</DIV>
</td>
<td colspan='6' valign='top' align='left'>
<DIV id='win0divG_FORM_I9_G_POSTALlbl'>
<label for='G_FORM_I9_G_POSTAL' id='G_FORM_I9_G_POSTAL_LBL' class='PSEDITBOXLABEL'>*Zip Code</label>
</DIV>
</td>
</tr>
<tr>
<td height='32' colspan='2'></td>
<td colspan='20' nowrap='nowrap' valign='top' align='left'>
<DIV id='win0divG_FORM_I9_G_ADDRESS'>
<input type='text' name='G_FORM_I9_G_ADDRESS' id='G_FORM_I9_G_ADDRESS' tabindex='47' value="" class='PSEDITBOX' style='width:324px; ' maxlength='50' aria-required='true' />
</DIV>
</td>
<td colspan='6' nowrap='nowrap' valign='top' align='left'>
<DIV id='win0divG_FORM_I9_G_APARTMENT_NBR'>
<input type='text' name='G_FORM_I9_G_APARTMENT_NBR' id='G_FORM_I9_G_APARTMENT_NBR' tabindex='48' value="" class='PSEDITBOX' style='width:81px; ' maxlength='10' />
</DIV>
</td>
<td colspan='8' nowrap='nowrap' valign='top' align='left'>
<DIV id='win0divG_FORM_I9_G_CITY'>
<input type='text' name='G_FORM_I9_G_CITY' id='G_FORM_I9_G_CITY' tabindex='49' value="" class='PSEDITBOX' style='width:120px; ' maxlength='30' aria-required='true' />
</DIV>
</td>
<td colspan='5' nowrap='nowrap' valign='top' align='left'>
<DIV id='win0divG_FORM_I9_G_STATE'>
<input type='text' name='G_FORM_I9_G_STATE' id='G_FORM_I9_G_STATE' tabindex='50' value="" class='PSEDITBOX' style='width:53px; ' maxlength='6' onchange="addchg_win0(this);oChange_win0=this;" aria-required='true' />
<a class='PSHYPERLINK' name='G_FORM_I9_G_STATE$prompt' id='G_FORM_I9_G_STATE$prompt' tabindex='51' href="javascript:pAction_win0(document.win0,'G_FORM_I9_G_STATE$prompt');">
<img src="/cs/ps/cache2/PT_PROMPT_LOOKUP_1.gif" alt='Look up State (Alt+5)' title='Look up State (Alt+5)' border='0' align='absmiddle' />
</a>
</DIV>
</td>
<td colspan='5' nowrap='nowrap' valign='top' align='left'>
<DIV id='win0divG_FORM_I9_G_POSTAL'>
<input type='text' name='G_FORM_I9_G_POSTAL' id='G_FORM_I9_G_POSTAL' tabindex='52' value="" class='PSEDITBOX' style='width:80px; ' maxlength='12' onchange="addchg_win0(this);oChange_win0=this;" aria-required='true' />
</DIV>
</td>
</tr>
<tr>
<td height='16'></td>
<td colspan='9' valign='top' align='left'>
<DIV id='win0divG_FORM_I9_G_BIRTHDATElbl'>
<label for='G_FORM_I9_G_BIRTHDATE' id='G_FORM_I9_G_BIRTHDATE_LBL' class='PSEDITBOXLABEL'>*Date of Birth (mm/dd/yyyy)</label>
</DIV>
</td>
<td colspan='13' valign='top' align='left'>
<DIV id='win0divG_FORM_I9_G_SSNlbl'>
<label for='G_FORM_I9_G_SSN' id='G_FORM_I9_G_SSN_LBL' class='PSEDITBOXLABEL'>*U.S. Social Security Number</label>
</DIV>
</td>
<td colspan='14' valign='top' align='left'>
<DIV id='win0divG_FORM_I9_G_EMAILIDlbl'>
<label for='G_FORM_I9_G_EMAILID' id='G_FORM_I9_G_EMAILID_LBL' class='PSEDITBOXLABEL'>Employee's E-mail Address</label>
</DIV>
</td>
<td colspan='9' valign='top' align='left'>
<DIV id='win0divG_FORM_I9_G_PHONElbl'>
<label for='G_FORM_I9_G_PHONE' id='G_FORM_I9_G_PHONE_LBL' class='PSEDITBOXLABEL'>Employee's Telephone Number</label>
</DIV>
</td>
</tr>
<tr>
<td height='9' colspan='2'></td>
<td colspan='9' rowspan='2' nowrap='nowrap' valign='top' align='left'>
<DIV id='win0divG_FORM_I9_G_BIRTHDATE'>
<input type='text' name='G_FORM_I9_G_BIRTHDATE' id='G_FORM_I9_G_BIRTHDATE' tabindex='53' value="05/05/1955" class='PSEDITBOX' style='width:71px; ' maxlength='10' onchange="addchg_win0(this);return doEdits_win0(this,'DMDY/450','N','N','N','N','N','N',0);"
onkeyup="if (isPromptKey(event))DatePrompt_win0('G_FORM_I9_G_BIRTHDATE','G_FORM_I9_G_BIRTHDATE','450',false);return false;" aria-required='true' />
<a class='PSHYPERLINK' name='G_FORM_I9_G_BIRTHDATE$prompt' id='G_FORM_I9_G_BIRTHDATE$prompt' tabindex='54' onfocus='doFocus_win0(this,true,false);' href="javascript:DatePrompt_win0('G_FORM_I9_G_BIRTHDATE','G_FORM_I9_G_BIRTHDATE$prompt','450',false);">
<img src="/cs/ps/cache2/PT_CALENDAR_1.gif" alt='Choose a date (Alt+5)' title='Choose a date (Alt+5)' border='0' align='absmiddle' />
</a>
</DIV>
</td>
<td colspan='6' rowspan='2' nowrap='nowrap' valign='top' align='left'>
<DIV id='win0divG_FORM_I9_G_SSN'>
<input type='password' name='G_FORM_I9_G_SSN' id='G_FORM_I9_G_SSN' tabindex='55' value="888881125" class='PSEDITBOX' style='width:74px; ' maxlength='9' onchange="addchg_win0(this);oChange_win0=this;" aria-required='true' />
</DIV>
</td>
<td colspan='6'></td>
<td colspan='16' rowspan='2' nowrap='nowrap' valign='top' align='left'>
<DIV id='win0divG_FORM_I9_G_EMAILID'>
<input type='text' name='G_FORM_I9_G_EMAILID' id='G_FORM_I9_G_EMAILID' tabindex='58' value="" class='PSEDITBOX' style='width:232px; ' maxlength='60' />
</DIV>
</td>
<td colspan='7' rowspan='2' nowrap='nowrap' valign='top' align='left'>
<DIV id='win0divG_FORM_I9_G_PHONE'>
<input type='text' name='G_FORM_I9_G_PHONE' id='G_FORM_I9_G_PHONE' tabindex='59' value="" class='PSEDITBOX' style='width:179px; ' maxlength='24' onchange="addchg_win0(this);oChange_win0=this;" />
</DIV>
</td>
</tr>
<tr>
<td height='23' colspan='2'></td>
<td colspan='6' valign='top' align='left'>
<DIV id='win0divG_FORM_I9_DRV_G_SSN'><span id='G_FORM_I9_DRV_G_SSN$span' onMouseOver="MOpopupObj_win0.StartPopup('win0divG_FORM_I9_DRV_G_SSN', 'G_FORM_I9_DRV_G_SSN_G_POPUP_SSN', 406, 90);" onMouseOut="MOpopupObj_win0.StopPopup(); " class='PSHYPERLINK' style='border-bottom:1px dashed black;padding-bottom:1px;display:inline-block;'><a name='G_FORM_I9_DRV_G_SSN' id='G_FORM_I9_DRV_G_SSN' ptlinktgt='pt_peoplecode' tabindex='56' onclick='javascript:cancelBubble(event);' href="javascript:submitAction_win0(document.win0,'G_FORM_I9_DRV_G_SSN');" class='PSHYPERLINK' >View</a></span>
</DIV>
<div id="G_FORM_I9_DRV_G_SSN_G_POPUP_SSN" style="visibility:hidden; position:absolute; top:0; left:0;">
<DIV class='ps_pspagecontainer' id='win0divPSPAGECONTAINER_G_POPUP_SSN'>
<table role='presentation' border='0' id='ACE_width' cellpadding='0' cellspacing='0' class='PSPAGECONTAINER' cols='2' width='379'>
<tr>
<td width='16' height='8'></td>
<td width='363'></td>
</tr>
<tr>
<td height='56'></td>
<td valign='top' align='left'>
<DIV id='win0divG_FORM_I9_'>
<table role='presentation' border='0' id='ACE_G_FORM_I9_' cellpadding='0' cellspacing='0' cols='3' width='363' class='PAGROUPBOXLABELINVISIBLE' style='border-style:none'>
<tr>
<td width='36' height='10'></td>
<td width='121'></td>
<td width='206'></td>
</tr>
<tr>
<td height='1'></td>
<td rowspan='2' valign='top' align='left'>
<DIV id='win0divG_FORM_I9_G_SSN$9$lbl'><span class='PSEDITBOXLABEL'>Social Security #</span>
</DIV>
</td>
</tr>
<tr>
<td height='27'></td>
<td valign='top' align='left'>
<DIV id='win0divG_FORM_I9_G_SSN$9$'><span class='PSEDITBOX_DISPONLY' id='G_FORM_I9_G_SSN$9$'>888881125</span>
</DIV>
</td>
</tr>
</table>
</DIV>
</td>
</tr>
<tr>
<td height='8' colspan='2'></td>
</tr>
</table>
</DIV>
</DIV>
</td>
</tr>
<!-- OMITTED -->
</table>
Running the Selenium Code
Now, I am trying to enter data into this form, but I keep getting the above exception. When I come to this page, I first am passing through a SSO page to authenticate me. It then redirects me to the form. No problems there. This is my code for doing this (Note that data is a POJO with data in it, and driver is a ChromeWebDriver).
driver.switchTo().frame("TargetContent");
driver.findElement(By.id("G_FORM_I9_G_LAST_NAME")).clear();
driver.findElement(By.id("G_FORM_I9_G_LAST_NAME")).sendKeys(data.lastName, Keys.TAB);
driver.findElement(By.id("G_FORM_I9_G_FIRST_NAME")).clear();
driver.findElement(By.id("G_FORM_I9_G_FIRST_NAME")).sendKeys(data.firstName, Keys.TAB);
driver.findElement(By.id("G_FORM_I9_G_ADDRESS")).clear();
driver.findElement(By.id("G_FORM_I9_G_ADDRESS")).sendKeys(data.address, Keys.TAB);
driver.findElement(By.id("G_FORM_I9_G_CITY")).clear();
driver.findElement(By.id("G_FORM_I9_G_CITY")).sendKeys(data.city, Keys.TAB);
driver.findElement(By.id("G_FORM_I9_G_STATE")).clear();
driver.findElement(By.id("G_FORM_I9_G_STATE")).sendKeys(data.state, Keys.TAB);
driver.findElement(By.id("G_FORM_I9_G_POSTAL")).clear();
driver.findElement(By.id("G_FORM_I9_G_POSTAL")).sendKeys(data.postal, Keys.TAB);
driver.findElement(By.id("G_FORM_I9_G_EMAILID")).clear();
driver.findElement(By.id("G_FORM_I9_G_EMAILID")).sendKeys(data.email, Keys.TAB);
driver.findElement(By.id("G_FORM_I9_G_CITIZEN_CHK")).click();
driver.findElement(By.id("G_FORM_I9_DRV_G_EE_SIGNATURE_PB")).click();
I get the error at various points throughout the process. Sometimes it will fail at the first field it tries to put text into, sometimes the second, and sometimes a later field.
Attempted Solutions
To combat this problem, I have tried calling this method:
private void waitForLoad(WebDriver driver)
{
new WebDriverWait(driver, 30)
.until((ExpectedCondition<Boolean>) wd -> ((JavascriptExecutor) wd)
.executeScript("return document.readyState").equals("complete"));
}
I have also tried this:
new WebDriverWait(driver, 5).until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath("//*[starts-with(#id, \"G_FORM_I9_\")]")));
What am I missing? What else do I need to check to ensure that the fields are actually accessible? I have tried debugging this in IntelliJ many times, but I suspect the act of stopping at a breakpoint loads the page, so I never see the error, because no elements are stale.
It looks like sometimes calling clear() changes your element reference, and while the page is updating, you are trying to locate this element again and finding the old reference, which causes a StaleElementReferenceException when you sendKeys().
If you already know the fields you're getting the problem with, you can try the following:
WebDriverWait wait = new WebDriverWait (driver, 30);
WebElement element = driver.findElement(By.id("G_FORM_I9_G_LAST_NAME"));
element.clear();
wait.until(ExpectedConditions.stalenessOf(element));
driver.findElement(By.id("G_FORM_I9_G_LAST_NAME")).sendKeys(data.lastName, Keys.TAB);
But if you don't know which fields are causing the problem, and the element doesn't become stale, you will get a TimeoutException.

Jump between dynamic textfield

I want to jump between textfields, but these fields are generated dynamically through an iterator from struts-tags:
<s:iterator value="aList">
<td width="50px" align="center">
<s:textfield name="solField" size="2" maxlength="1" style="text-transform: uppercase; text-align:center"/>
</td>
</s:iterator>
I tried jumping with javascript but having the same field name is not working properly.
The code in the browser is (with three items in the list to iterate):
<td width="50px" align="center">
<input type="text" name="solField" size="2" maxlength="1" value="" id="correct_solField" style="text-transform: uppercase; text-align:center"/>
</td>
<td width="50px" align="center">
<input type="text" name="solField" size="2" maxlength="1" value="" id="correct_solField" style="text-transform: uppercase; text-align:center"/>
</td>
<td width="50px" align="center">
<input type="text" name="solField" size="2" maxlength="1" value="" id="correct_solField" style="text-transform: uppercase; text-align:center"/>
</td>
Any ideas?
Thanks in advance
How about this? (using jQuery)
$(document).on('keyup', 'input', function(){
if($(this).val().length >= $(this).attr('maxlength'))
{
$(this).nextAll('input:first').focus();
// OR even
// $(this).next('input').focus();
}
});
Is that what you're looking for? http://fiddle.jshell.net/C3jeY/

Connect to a form with changing values with HttpClient in Java

I'm trying to connect to a URL with a form that has this values:
http://aulavirtual.uv.es/
How can I do this if every time I log value changes (time, token_id, hash) ? Please, help me !! I try with HttpUrlConnection and now with HttpClient 4.
<form name="login" method="post" action="/register/">
<input type="hidden" name="form:mode" value="edit">
<input type="hidden" name="form:id" value="login">
<input type="hidden" name="__confirmed_p" value="0">
<input type="hidden" name="__refreshing_p" value="0">
<input type="hidden" name="return_url" value="/dotlrn/index">
<input type="hidden" name="time" value="1352222384">
<input type="hidden" name="token_id" value="423">
<input type="hidden" name="hash" value="3F8865DB5E6603C296428A5A7C0D66C0256D5626">
<tr>
<td class="formulari">Nom d'usuari</td>
<td class="formulari">
<input type="text" id="username" name="username" value="" size="25" alt="Nom d'usuari"></td>
</tr>
<tr>
<TD class="formulari">Contrasenya</TD>
<TD class="formulari">
<input type="password" id="password" name="password" size="25" ALT="Contrasenya d'accés"></TD>
</tr>
<TR>
<TD COLSPAN="2" class="formulari1">
<div align="center">
<input id="login" type="submit" value=" Entreu " alt="Entrar al Aula Virtual">
<IMG BORDER="0" HEIGHT="25" WIDTH="25" SRC="/resources/key.gif" ALT="Entrar en modo seguro" TITLE="Entrar en modo seguro">
</div>
<!--
<Table COLSPAN="2" class="fonsgifs">
<div align="center" class="titol3">
<strong><U>Altres cursos acadèmics</U></SPAN></strong>
</table>
<BR><BR><BR>
<CENTER>
-->
<!--
<img align="center" src="/images/cabeceraOPEN.png" border = 0 align="center" alt="Conferencia OPEN" />
<BR><img align="center" src="/images/ConferenciaSofwareLibre.png" border = 0 align="center" alt="Conferencia Software Libre" />
<BR>7th OpenACS / .LRN Conference
<BR>Conferencia sobre Software Libre en Educación Superior
</CENTER>
-->
</TD>
</TR>
<TR>
<TD class="formulari1" COLSPAN="2" style="text-align:center">
<BR><BR><BR>
</TD>
</TR>
</FORM>
This form uses hidden parameters for security purposes. Every time http://aulavirtual.uv.es/ is displayed these hidden parameters change.
You could try changing your code to do the following:
GET the http://aulavirtual.uv.es/ page.
Parse the resulting HTML to extract the values of the time, token_id, and hash hidden parameters from the form element.
Do the POST as you do now, but pass in these three values instead of the fixed values you are using now.

How do I create a multidimensional http-post array in JSP?

I'm trying to submit (post) a multidimensional (2D) html-form to a JSP.
In HTML there's the following code:
<tr class="odd">
<td>
<input type="checkbox" name="row1['attribute']" value="Druckerei" id="attribute-1" />
</td>
<td>
<label for="attribute-1">Druckerei</label>
</td>
<td>
<input type="text" name="row1['weight']" size="3" value="0" />
</td>
</tr>
<tr class="even">
<td>
<input type="checkbox" name="row2['attribute']" value="Erscheinungstag" checked="checked" id="attribute-2" />
</td>
<td>
<label for="attribute-2">Erscheinungstag</label>
</td>
<td>
<input type="text" name="row2['weight']" size="3" value="0" />
</td>
</tr>
Is there in JSP a way to handle the 2D post data (perhaps similar to PHP)?
Would it be easier if I had different HTML-Code?
Thank you very much!
No you cannot have 2D array like Rows and Columns. But you can achieve this by sending multiple values for a POST data parameter which works like this:
Your Form might look like this:
<tr class="odd">
<td>
<input type="checkbox" name="attribute" value="Druckerei" id="attribute-1" />
</td>
<td>
<label for="attribute-1">Druckerei</label>
</td>
<td>
<input type="text" name="weight" size="3" value="0" />
</td>
</tr>
<tr class="even">
<td>
<input type="checkbox" name="attribute" value="Erscheinungstag"
checked="checked" id="attribute-2" />
</td>
<td>
<label for="attribute-2">Erscheinungstag</label>
</td>
<td>
<input type="text" name="weight" size="3" value="0" />
</td>
</tr>
And your POST Data will be sent like:
url?attribute=Druckerei,Erscheinungstag&weight=3,3
From the servlets you can access the the POST parameter like:
String[] attributes = request.getParameterValues("attribute");
String[] weights = request.getParameterValues("weight");

Categories