按索引定位元素对于具有类似属性的元素不起作用

时间:2014-11-04 09:25:11

标签: java selenium textbox selenium-webdriver automation

我正在自动化一个Web应用程序,我需要在其中输入大约20个具有相似属性的文本框,下面是整页HTML代码。

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<!--[if lt IE 7]> <html class="lt-ie9 lt-ie8 lt-ie7" lang="en"> <![endif]-->
<!--[if IE 7]> <html class="lt-ie9 lt-ie8" lang="en"> <![endif]-->
<!--[if IE 8]> <html class="lt-ie9" lang="en"> <![endif]-->
<!--[if gt  IE 9]> <html class="lt-ie9" lang="en"> <![endif]-->
<!--[if gt IE 8]>
<html lang="en"> 
<![endif]-->
<html>
<head>
	<meta http-equiv="X-UA-Compatible" content="IE=11" />
	<link rel="shortcut icon" href="favicon.ico" type="image/x-icon">
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<META HTTP-EQUIV="Cache-Control" CONTENT="max-age=36000000, public">
	<title>Table Administration Row Add Page</title>
	
    <link rel="stylesheet" href="css/new.css">
     <link rel="stylesheet" href="css/demo_table_jui.css">
     <link rel="stylesheet" href="css/jquery-ui-1.8.4.custom.css">
	 <link rel="stylesheet" href="css/time.css">
	

	<style type="text/css">
	ul#css3menu1,ul#css3menu1 ul{
		margin:0;list-style:none;padding:0;}
	ul#css3menu1,ul#css3menu1 .submenu{
		background-color:#FFFFFF;border-width:1px;border-style:solid;border-color:#b2b2b4;-moz-border-radius:5px 5px 0 0;-webkit-border-radius:5px 5px 0 0;border-radius:5px 5px 0 0;}
	ul#css3menu1 .submenu{
		visibility:hidden;position:absolute;left:-1px;top:100%;opacity:0;-moz-transition:all 0.5s;-webkit-transition:opacity 0.5s;-o-transition:opacity 0.5s,visibility 0.5s;transition:opacity 0.5s;float:left;z-index:2;background-color:#FFFFFF;background-image:-o-linear-gradient(-90deg,rgba(44,160,202,0),rgba(0,0,0,0.1)); background-image:-moz-linear-gradient(-90deg,rgba(44,160,202,0),rgba(0,0,0,0.1));   background-image:linear-gradient(180deg,rgba(44,160,202,0),rgba(0,0,0,0.1));  background-image:-webkit-linear-gradient(-90deg,rgba(44,160,202,0),rgba(0,0,0,0.1));border-width:1px;border-radius:0px 0px 4px 4px;-moz-border-radius:0px 0px 4px 4px;-webkit-border-radius:0px;-webkit-border-bottom-right-radius:4px;-webkit-border-bottom-left-radius:4px;border-style:solid;border-color:#FFFFFF #bfc1c2 #bfc1c2 #bfc1c2;-moz-box-shadow:0 5px 5px rgba(20, 20, 25, 0.2), 0 0 0 1px #FFFFFF inset;-webkit-box-shadow:0 5px 5px rgba(20, 20, 25, 0.2), 0 0 0 1px #FFFFFF inset;box-shadow:0 5px 5px rgba(20, 20, 25, 0.2), 0 0 0 1px #FFFFFF inset;padding:5px;}
	ul#css3menu1 li:hover>.submenu{
		visibility:visible;opacity:1;}
	ul#css3menu1 li{
		position:relative;display:block;white-space:nowrap;font-size:0;float:left;}
	ul#css3menu1 li:hover{
		z-index:1;}
	ul#css3menu1 ul .submenu{
		position:absolute;left:99%;top:0;border-radius:6px;-moz-border-radius:6px;-webkit-border-radius:6px;border-color:#bfc1c2;}
	ul#css3menu1{
		font-size:0;z-index:999;position:relative;display:inline-block;zoom:1;padding:0;-moz-box-shadow:0 2px 0 #bfc1c2;-webkit-box-shadow:0 2px 0 #bfc1c2;box-shadow:0 2px 0 #bfc1c2;
		*display:inline;}
	ul#css3menu1 .column{
		float:left;}
	* html ul#css3menu1 li a{
		display:inline-block;}
	ul#css3menu1>li{
		margin:0;}
	ul#css3menu1 a:active, ul#css3menu1 a:focus{
		outline-style:none;}
	ul#css3menu1 a{
		display:block;vertical-align:middle;text-align:left;text-decoration:none;font:14px 'Droid Sans', "Lucida Sans Unicode", "Lucida Grande", sans-serif;color:#92979E;cursor:pointer;padding:13px 15px;background-color:;background-image:-o-linear-gradient(-90deg,rgba(255,255,255,0),rgba(85,85,85,0.13)); background-image:-moz-linear-gradient(-90deg,rgba(255,255,255,0),rgba(85,85,85,0.13));   background-image:linear-gradient(180deg,rgba(255,255,255,0),rgba(85,85,85,0.13));  background-image:-webkit-linear-gradient(-90deg,rgba(255,255,255,0),rgba(85,85,85,0.13));background-repeat:repeat;background-position:0 0;border-width:0px;border-style:none;border-color:;filter:progid:DXImageTransform.Microsoft.gradient(gradientType=0,startColorstr=#00FFFFFF,endColorstr=#21555555)}
	ul#css3menu1 ul li{
		float:none;margin:0;}
	ul#css3menu1 ul a{
		text-align:left;padding:5px;-moz-box-shadow:none;-webkit-box-shadow:none;box-shadow:none;background-color:none;background-image:none;border-width:0;border-style:none;border-radius:6px;-moz-border-radius:6px;-webkit-border-radius:6px;color:#92979E;text-decoration:none;}
	ul#css3menu1 li:hover>a,ul#css3menu1 li a.pressed{
		border-style:none;color:#92979E;background-image:-o-linear-gradient(-90deg,rgba(85,85,85,0.13),rgba(255,255,255,0)); background-image:-moz-linear-gradient(-90deg,rgba(85,85,85,0.13),rgba(255,255,255,0));   background-image:linear-gradient(180deg,rgba(85,85,85,0.13),rgba(255,255,255,0));  background-image:-webkit-linear-gradient(-90deg,rgba(85,85,85,0.13),rgba(255,255,255,0));background-position:0 100%;text-decoration:none;filter:progid:DXImageTransform.Microsoft.gradient(gradientType=0,startColorstr=#21555555,endColorstr=#00FFFFFF)}
	ul#css3menu1 img{
		border:none;vertical-align:middle;margin-right:13px;}
	ul#css3menu1 ul span{
		background-image:none;padding-right:5px;}
	ul#css3menu1 ul li:hover>a,ul#css3menu1 ul li a.pressed{
		-moz-box-shadow:0 1px 0 #CF7E4C;-webkit-box-shadow:0 1px 0 #CF7E4C;box-shadow:0 1px 0 #CF7E4C;background-color:#ef9d58;background-image:-o-linear-gradient(-90deg,rgba(255,255,255,0),rgba(85,85,85,0.13)); background-image:-moz-linear-gradient(-90deg,rgba(255,255,255,0),rgba(85,85,85,0.13));   background-image:linear-gradient(180deg,rgba(255,255,255,0),rgba(85,85,85,0.13));  background-image:-webkit-linear-gradient(-90deg,rgba(255,255,255,0),rgba(85,85,85,0.13));border-style:none;color:#FFFFFF;text-decoration:none;filter:progid:DXImageTransform.Microsoft.gradient(gradientType=0,startColorstr=#00FFFFFF,endColorstr=#21555555)}
	ul#css3menu1 li.topfirst>a{
		-moz-box-shadow:0 0 0 1px #FFFFFF inset;-webkit-box-shadow:0 0 0 1px #FFFFFF inset;box-shadow:0 0 0 1px #FFFFFF inset;background-color:#FFFFFF;border-width:0 1px 0 0;border-style:solid;border-color:#bfc1c2;border-radius:5px 0 0 0;-moz-border-radius:5px 0 0 0;-webkit-border-radius:5px;-webkit-border-top-right-radius:0;-webkit-border-bottom-right-radius:0;-webkit-border-bottom-left-radius:0;text-shadow:0 1px 0 #FFFFFF;}
	ul#css3menu1 li.topfirst:hover>a,ul#css3menu1 li.topfirst a.pressed{
		-moz-box-shadow:0 4px 6px -2px rgba(0,20,50, 0.26) inset;-webkit-box-shadow:0 4px 6px -2px rgba(0,20,50, 0.26) inset;box-shadow:0 4px 6px -2px rgba(0,20,50, 0.26) inset;background-color:#FFFFFF;border-style:solid;border-color:#bfc1c2;text-shadow:0 1px 0 #FFFFFF;}
	ul#css3menu1 li.topmenu>a{
		-moz-box-shadow:0 0 0 1px #FFFFFF inset;-webkit-box-shadow:0 0 0 1px #FFFFFF inset;box-shadow:0 0 0 1px #FFFFFF inset;background-color:#FFFFFF;border-width:0 1px 0 0;border-style:solid;border-color:#bfc1c2;text-shadow:0 1px 0 #FFFFFF;}
	ul#css3menu1 li.topmenu:hover>a,ul#css3menu1 li.topmenu a.pressed{
		-moz-box-shadow:0 4px 6px -2px rgba(0,20,50, 0.26) inset;-webkit-box-shadow:0 4px 6px -2px rgba(0,20,50, 0.26) inset;box-shadow:0 4px 6px -2px rgba(0,20,50, 0.26) inset;background-color:#FFFFFF;border-style:solid;border-color:#bfc1c2;text-shadow:0 1px 0 #FFFFFF;}
	ul#css3menu1 li.toplast>a{
		-moz-box-shadow:0 0 0 1px #FFFFFF inset;-webkit-box-shadow:0 0 0 1px #FFFFFF inset;box-shadow:0 0 0 1px #FFFFFF inset;background-color:#FFFFFF;border-width:0px;border-style:solid;border-color:#bfc1c2;border-radius:0 5px 0 0;-moz-border-radius:0 5px 0 0;-webkit-border-radius:0;-webkit-border-top-right-radius:5px;text-shadow:0 1px 0 #FFFFFF;}
	ul#css3menu1 li.toplast:hover>a,ul#css3menu1 li.toplast a.pressed{
		-moz-box-shadow:0 4px 6px -2px rgba(0,20,50, 0.26) inset;-webkit-box-shadow:0 4px 6px -2px rgba(0,20,50, 0.26) inset;box-shadow:0 4px 6px -2px rgba(0,20,50, 0.26) inset;background-color:#FFFFFF;border-style:solid;border-color:#bfc1c2;text-shadow:0 1px 0 #FFFFFF;}
	</style>
	<script type='text/javascript' src='js/jquery-1.92.js'></script>
	<script type='text/javascript' src='js/jqueryshim.js'></script>
	<script type="text/javascript" src="js/jquery.dataTables.min.js"></script>
	<script type="text/javascript" src="js/ddslick.js"></script>
    <script  type="text/javascript"  src="js/jqueryui-min.js"></script>
	<script  type="text/javascript"  src="js/jquerytimeaddon.js"></script>
	<script type="text/javascript">
	function checkSubmitSearch(e) {
		if (e.keyCode == 13)
			quickSearch();
		else
			return false;
	}
	function quickSearch() {
		if (document.getElementById("txtQuickSearch").value == ""
				|| isNaN(document.getElementById("txtQuickSearch").value)) {
			alert("Please enter a valid Template ID");
		} else {
			window.location = "templateModify.action?templateID="
			+ document.getElementById("txtQuickSearch").value + "&Search=y";
		}

	}
</script>
</head>
	<body>
			

<script type="text/javascript">
	function selectLanguage(value) {
		document.location.href = "locale.action?request_locale=" + value;
		document.forms[0].submit();
	}
	
	function searchHelp() {
	
		window.open('/AdminPortal/jsp/SearchHelp.jsp#RTAdmin', "_blank","toolbar=yes, location=yes, directories=no, status=no, menubar=yes, scrollbars=yes, resizable=no, copyhistory=yes, width=840, height=630");
					}
					
function templateHelp() {
	
		window.open('/AdminPortal/jsp/TemplateAdministartionHelp.jsp#RTAgent', "_blank","toolbar=yes, location=yes, directories=no, status=no, menubar=yes, scrollbars=yes, resizable=no, copyhistory=yes, width=840, height=630");
					}
					
function help() {
		window.open('/AdminPortal/jsp/Help.jsp', '',
							'height=630,width=840,toolbar=no,directories=no,status=no,menubar=no,scrollbars=yes,resize:no,overflow:auto');
					}	
</script>
<div class="containermain">
	<span style="float: right">Logged in as :<a
		href="userDetails.action?userno=636">Testaccount2@trx.com</a><br>
		&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
	    		<a href=""onclick="return templateHelp()">Help</a>		    		
		</span>
	<!--   		
	 -->
	<!--<a href="/AdminPortal/locale.action?request_locale=en">English</a>
	<a href="/AdminPortal/locale.action?request_locale=es">Spanish</a>
	<a href="/AdminPortal/locale.action?request_locale=fr">France</a>
	<a href="/AdminPortal/locale.action?request_locale=de">Germany</a>-->

	<table>
		<tr>
		<td>
			<!--<select name="language" id="language" onchange="selectLanguage(this.value)"><option value="-1" onchange="selectLanguage(this.value)">Select Language</option><option value="en" onchange="selectLanguage(this.value)">English</option><option value="es" onchange="selectLanguage(this.value)">Spanish</option><option value="fr" onchange="selectLanguage(this.value)">France</option><option value="de" onchange="selectLanguage(this.value)">Germany</option><option value="fi" onchange="selectLanguage(this.value)">Finnish</option></select>-->
		</td>
		</tr>
		</table>
			<div id="menu">
				<ul id="css3menu1" class="topmenu">
					<li class="topfirst"><a href="welcome.action" title="Home"
						style="height: 24px; line-height: 24px;">Home</a></li>
					<li class="topmenu"><a href="#" title="Configurations"
						style="height: 24px; line-height: 24px;"><span>Configuration</span></a>
						<div class="submenu" style="width: 230px;">
							<div class="column" style="width: 100%">
								<ul>
									
									<li><a href="appConfig">Configure Applications </a></li>
									
									<li><a href="hierarchyMatadataDetails">Configure Hierarchy MetaData </a></li>
									
									<li><a href="hierarchyData">Configure Hierarchy Data </a></li>
									
									<li><a href="userList">User Management </a></li>

									<li><a href="roleList">Role Definition </a></li>

								</ul>
							</div>
						</div></li>

					
					<li class="topmenu"><a href="templateMapping"
						title="Mapping Template" style="height: 24px; line-height: 24px;"><span>Mapping Template</span></a></li>
					
					<li class="topmenu"><a href="settings"
						title="Settings" style="height: 24px; line-height: 24px;"><span>Settings</span></a></li>
					
					<li class="topmenu"><a href="mailSearchApplication"
						title="Search" style="height: 24px; line-height: 24px;"><span>Search</span></a></li>
					
					<li class="topmenu"><a href="#" title="Operations"
						style="height: 24px; line-height: 24px;"><span>Operations</span></a>
						<div class="submenu" style="width: 230px;">
							<div class="column" style="width: 100%">
								<ul>
									

									<li><a href="preprocessorApplication">PreProcessor </a></li>
									

									<li><a href="systemProperties">System Information </a></li>
									

									<li><a href="volumeStatsPage">Volume Stats </a></li>
									

									<li><a href="searchLogsPage">System Logs </a></li>
									

									<li><a href="tableAdministration.action">Table Operations </a></li>
									

									<li><a href="templateCustom">Template Administration </a></li>
									

									<li><a href="requestHandling.action">Request Handling </a></li>
									
							<!--commenting the Resend Email Requests part and  Generate Email Requests as of now, it might get used in feature -->
							<!--<li><a href="resendEmailsRequestsPage" title="Resend Email Request">Resend Email Requests</a></li>							
							<li><a href="generateEmailsRequestsPage" title="Generate Email Requests">Generate Email Requests</a></li>-->
					<!--commenting the Application Transformations part -->
					<<li><a href="applicationTransformationsPage" title="ApplicationTransformations">Application Transformations</a></li>
								</ul>
							</div>
						</div></li>
					

					
					<li class="toplast"><a
						href="/AdminPortal/j_spring_security_logout"
						style="height: 24px; line-height: 24px;">SignOut</a></li>
				</ul>

			</div>
			
			<div class="quicksearch">
				<span style="float: left; padding-left: 10px;"> <input
					type="text" value="" id="txtQuickSearch" name="txtQuickSearch"
					onkeypress="checkSubmitSearch(event)" placeholder='Quick Search' />
				</span>
			</div>

			
	 <!--  <div><input value="Help" onclick="return help()"/>
	</div>
	<a href="/AdminPortal/jsp/Help.jsp">Help</a>-->

			<div style="clear: both"></div>

<script>
	function cancelAdd() {
		document.forms[0].action = "tableAdministrationSearch.action";
		document.forms[0].submit();
	}
</script>

<div >
	<h3>
		Table Operations
	</h3>
	<p>
		User can Insert Values
	</p>
	<p>
		Please leave the textbox blank if null needs to be inserted
	</p>
	

	
	<form name="tableAdministrationAddSave" id="tableAdministrationAddSave" action="/AdminPortal/tableAdministrationAddSave.action" method="post">
	<table width="100%" align="center">
	<tr>
	<td>

		<label class="labelboldClass" for="name">Table Name
			TRANS_RAIL_STATIONS </label>
		
			<table width="100%">
				
					<tr>
						<td align="left" width="20%">RAILSTATION_CODE:</td>
						<td width="80%"><input type="text" name="rowValue" style="position:relative"></td>
					</tr>
				
					<tr>
						<td align="left" width="20%">RAILSTATION_NAME_UC:</td>
						<td width="80%"><input type="text" name="rowValue" style="position:relative"></td>
					</tr>
				
					<tr>
						<td align="left" width="20%">RAILSTATION_NAME:</td>
						<td width="80%"><input type="text" name="rowValue" style="position:relative"></td>
					</tr>
				
					<tr>
						<td align="left" width="20%">COUNTRY:</td>
						<td width="80%"><input type="text" name="rowValue" style="position:relative"></td>
					</tr>
				
					<tr>
						<td align="left" width="20%">STATE:</td>
						<td width="80%"><input type="text" name="rowValue" style="position:relative"></td>
					</tr>
				
					<tr>
						<td align="left" width="20%">MAIN_CITY_NAME:</td>
						<td width="80%"><input type="text" name="rowValue" style="position:relative"></td>
					</tr>
				
					<tr>
						<td align="left" width="20%">en_US:</td>
						<td width="80%"><input type="text" name="rowValue" style="position:relative"></td>
					</tr>
				
					<tr>
						<td align="left" width="20%">en_UK:</td>
						<td width="80%"><input type="text" name="rowValue" style="position:relative"></td>
					</tr>
				
					<tr>
						<td align="left" width="20%">es_ES:</td>
						<td width="80%"><input type="text" name="rowValue" style="position:relative"></td>
					</tr>
				
					<tr>
						<td align="left" width="20%">fr_FR:</td>
						<td width="80%"><input type="text" name="rowValue" style="position:relative"></td>
					</tr>
				
					<tr>
						<td align="left" width="20%">de_DE:</td>
						<td width="80%"><input type="text" name="rowValue" style="position:relative"></td>
					</tr>
				
					<tr>
						<td align="left" width="20%">nl_NL:</td>
						<td width="80%"><input type="text" name="rowValue" style="position:relative"></td>
					</tr>
				
					<tr>
						<td align="left" width="20%">it_IT:</td>
						<td width="80%"><input type="text" name="rowValue" style="position:relative"></td>
					</tr>
				
					<tr>
						<td align="left" width="20%">fi_FI:</td>
						<td width="80%"><input type="text" name="rowValue" style="position:relative"></td>
					</tr>
				
					<tr>
						<td align="left" width="20%">df_DF:</td>
						<td width="80%"><input type="text" name="rowValue" style="position:relative"></td>
					</tr>
				
					<tr>
						<td align="left" width="20%">ru_RU:</td>
						<td width="80%"><input type="text" name="rowValue" style="position:relative"></td>
					</tr>
				
					<tr>
						<td align="left" width="20%">zh_CN:</td>
						<td width="80%"><input type="text" name="rowValue" style="position:relative"></td>
					</tr>**
				
					<tr>
						<td align="left" width="20%">fr_CA:</td>
						<td width="80%"><input type="text" name="rowValue" style="position:relative"></td>
					</tr>
				
			
			</table>
	</td>
	</tr>
	</table>

			<div class="submitWrapper" align="center">
				<input type="submit" value="Insert" id="tableAdministrationAddSave_0"></input>
				<input type="submit" value="Cancel" id="tableAdministrationAddSave_2" onclick="cancelAdd()"></input>
			</div>
		
	</form>
</div>
<script type="text/javascript" src="js/app.js"></script>

</div>

	</body>
</html>

我尝试使用下面的WebDriver代码为上面的文本框输入值,但是只输入第一个文本框并且获取错误没有这样的元素例外以进行扩孔。

 driver.findElement(By.xpath("//input[@name='rowValue'][1]")).sendKeys("Test1");
 driver.findElement(By.xpath("//input[@name='rowValue'][2]")).sendKeys("Test2");

如果有人能帮我输入所有文本框,那将非常有帮助。如果有人需要更多详细信息,请告诉我。

提前致谢!!!

3 个答案:

答案 0 :(得分:2)

快速修复 - 您可以使用webelements列表:

List<WebElement> listname = driver.findElements(By.xpath("//input[@name='rowValue']");
listname.get(1).sendKeys("Test1");
listname.get(2).sendKeys("Test2");

答案 1 :(得分:1)

如果这些不是在运行时生成的,您可以为所有html元素(此处为文本框)提供id并使用

.findElements(By.id) 

方法而不是

.findElements(By.xpath)

答案 2 :(得分:0)

您必须在xpath中添加括号,该括号应如下所示:(//input[@name='rowValue'])[?]