如何在多个文本框中插入值并在其中更新其中的值?

时间:2015-05-25 09:33:27

标签: java selenium webdriver

我的java代码

package com.ej.zob.modules;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

public class Revenue {
public void Execute(String value)
 {
       LaunchApplication.driver.findElement(By.linkText("VIEW")).click();
       LaunchApplication.driver.findElement(By.linkText("REVENUE")).click();
       LaunchApplication.driver.findElement(By.name("click")).click();
       LaunchApplication.driver.findElements(By.xpath("//a[contains(@id,'Edit_')")).click();

       List<WebElement> a = LaunchApplication.driver.findElements(By.xpath("//input[@type='text']"));
       List<WebElement> b = LaunchApplication.driver.findElements(By.xpath("//input[@value='Update']"));



       for(WebElement elem_1:a){
           elem_1.clear();            
           elem_1.sendKeys(value);
           }
       for(WebElement elem_2:b)
       {
           elem_2.click();

       }



       }


}

}

我的HTML

<tr>
<td>Arunachal Pradesh</td>
<td>
<div>
<input id="1_1" type="text" value="44155" style="width:50px" name="U1">
<br>
<input type="button" value="Update" onclick="fnUpdateRevenue("1_1")" style="width:60px">
</div>
</td>
<td>
<div>
<input id="1_2" type="text" value="79103" style="width:50px" name="U1">
<br>
<input type="button" value="Update" onclick="fnUpdateRevenue("1_2")" style="width:60px"> 
</div>
</td>
<td>
<div>
<input id="1_3" type="text" value="11639" style="width:50px" name="U1">
<br>
<input type="button" value="Update" onclick="fnUpdateRevenue("1_3")" style="width:60px">
</div>
</td>
<td>
<div>
<input id="1_4" type="text" value="22004" style="width:50px" name="U1">
<br>
<input type="button" value="Update" onclick="fnUpdateRevenue("1_4")" style="width:60px">
</div>
</td>
<td>
<div>
<input id="1_5" type="text" value="65958" style="width:50px" name="U1">
<br>
<input type="button" value="Update" onclick="fnUpdateRevenue("1_5")" style="width:60px">
</div>
</td>
<td>
 <div>
<input id="1_6" type="text" value="76837" style="width:50px" name="U1">
<br>
<input type="button" value="Update" onclick="fnUpdateRevenue("1_6")" style="width:60px">
</div>
</td>
<td>
<div>
<input id="1_7" type="text" value="3642" style="width:50px" name="U1">
<br>
<input type="button" value="Update" onclick="fnUpdateRevenue("1_7")" style="width:60px">
</div>
</td>
<td>
<div>
<input id="1_8" type="text" value="84573" style="width:50px" name="U1">
<br>
<input type="button" value="Update" onclick="fnUpdateRevenue("1_8")"  style="width:60px">
</div>
</td>
<td>
<div>
<input id="1_9" type="text" value="3438" style="width:50px" name="U1">
<br>
<input type="button" value="Update" onclick="fnUpdateRevenue("1_9")" style="width:60px">
</div>
</td>
<td>
<div>
<input id="1_10" type="text" value="32859" style="width:50px" name="U1">
<br>
<input type="button" value="Update" onclick="fnUpdateRevenue("1_10")" style="width:60px">
</div>
</td>
<td>
<div>
<input id="1_11" type="text" value="45793" style="width:50px" name="U1">
<br>
<input type="button" value="Update" onclick="fnUpdateRevenue("1_11")" style="width:60px">
</div>
</td>
<td>
<div>
<input id="1_12" type="text" value="95662" style="width:50px" name="U1">
<br>
<input type="button" value="Update" onclick="fnUpdateRevenue("1_12")" style="width:60px">
</div>
</td>
<td>
<a id="Edit_1" href="#" onclick="fnEditRevenue("1");" style="visibility: hidden;">Edit</a>
|
<a id="Hide_1" href="#" onclick="fnHideRevenue(1);" style="visibility: hidden;">Hide</a>
|
<a id="Show_1" href="#" style="visibility:hidden" onclick="fnShowRevenue(1);">Show</a>
</td>

让我们先了解我的应用程序的功能。在我的网页上有超过26行和12列。每行包含12个文本框和12个按钮以及&#34;编辑&#34;按钮也是。单击编辑按钮然后单击文本框并更新&#39;按钮将打开。这是我的网页功能。

我想要做的是点击编辑按钮然后在文本框中输入一些值并更新&#39;按钮应该被点击。这应该发生在每一行。通过使用上面的代码,我可以点击单个&#39;编辑&#39;按钮并仅更新单行。

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

由于您尚未根据列名进行识别,因此仅更新单行,请尝试此操作 -

 WebElement table = driver.findElement(By
            .cssSelector("table[id='yourtableid']"));
List<WebElement> col = table.findElements(By.tagName("td"));
for (int cnum = 0; cnum < col.size(); cnum++) {
WebElement text = driver.findElement(By.xpath("//input[@type='text']"));
text.clear();
text.sendKeys(value);
WebElement update = driver.findElement(By.xpath("//input[@value='update']"));
update.click();
}