无法通过selenium webdriver中的XPath选择特定列的文本框

时间:2015-05-31 08:42:14

标签: java selenium xpath selenium-webdriver webdriver

我的Java

package com.ej.zob.modules;

import java.util.List;

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

public class Revenue_Month {
public void Execute()
{
    LaunchApplication.driver.findElement(By.linkText("VIEW")).click();
    LaunchApplication.driver.findElement(By.linkText("REVENUE")).click();
    LaunchApplication.driver.findElement(By.name("click")).click();
    List<WebElement> c = LaunchApplication.driver.findElements(By.xpath("//a[contains(@id,'Edit_')]"));
    for(WebElement elem_1:c)
    {
        elem_1.click();
    }
    List<WebElement> w = LaunchApplication.driver.findElements(By.xpath("//td[position() = count(ancestor::table/tbody/tr/td[.='May']/preceding-sibling::*)+1]"));
    //for(WebElement elem:w)
    //{
    //  elem.
    //  elem.sendKeys("50");
    //  elem.click();
    //}

 }
 }

我的HTML

<div id="idshowtable" style="background:#7F4F20">
<table id="mainTable" border="1">
<tbody>
<tr>
<td align="center" colspan="14">State Wise Revenue of a Bank (all Figures are in INR and in Lacs)</td>
</tr>
<tr>
<td>State \ Month</td>
<td>
<a href="#" onclick="">October</a>
</td>
<td>
<a href="#" onclick="">November</a>
</td>
<td>
<a href="#" onclick="">December</a>
</td>
<td>
<a href="#" onclick="">January</a>
</td>
<td>
<a href="#" onclick="">February</a>
</td>
<td>
<a href="#" onclick="">March</a>
</td>
<td>
<a href="#" onclick="">April</a>
</td>
<td>
<a href="#" onclick="">May</a>
 </td>
<td>
<a href="#" onclick="">June</a>
</td>
<td>
<a href="#" onclick="">July</a>
</td>
<td>
<a href="#" onclick="">August</a>
</td>
<td>
<a href="#" onclick="">September</a>
</td>
<td>
<a href="#" onclick="">Operation</a>
</td>
</tr>
<tr>
<td>Rajasthan</td>
<td>
<div>
<input id="1_1" type="text" value="51838" 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="62192" 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="87617" 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="59826" 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="97075" 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="58818" 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="41250" 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="52233" 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="74272" 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="28197" 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="79977" 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="11158" 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>
</tr>
<tr>
<td>Sikkim</td>
<td>
<div>
<input id="2_1" type="text" value="66503" style="width:50px" name="U2">
<br>
<input type="button" value="Update" onclick="fnUpdateRevenue("2_1")" style="width:60px">
</div>
</td>
<td>
<div>
<input id="2_2" type="text" value="84033" style="width:50px" name="U2">
<br>
<input type="button" value="Update" onclick="fnUpdateRevenue("2_2")" style="width:60px">
</div>
</td>
<td>
<div>
<input id="2_3" type="text" value="37062" style="width:50px" name="U2">
<br>
<input type="button" value="Update" onclick="fnUpdateRevenue("2_3")" style="width:60px">
</div>
</td>
<td>
<div>
<input id="2_4" type="text" value="25780" style="width:50px" name="U2">
<br>
<input type="button" value="Update" onclick="fnUpdateRevenue("2_4")" style="width:60px">
</div>
</td>
<td>
<div>
<input id="2_5" type="text" value="70574" style="width:50px" name="U2">
<br>
<input type="button" value="Update" onclick="fnUpdateRevenue("2_5")" style="width:60px">
</div>
</td> 
<td>
<div>
<input id="2_6" type="text" value="99017" style="width:50px" name="U2">
<br>
<input type="button" value="Update" onclick="fnUpdateRevenue("2_6")" style="width:60px">
</div>
</td>
<td>
<div>
<input id="2_7" type="text" value="1406" style="width:50px" name="U2">
<br>
 <input type="button" value="Update" onclick="fnUpdateRevenue("2_7")" style="width:60px">
</div>
</td>
<td>
 <div>
 <input id="2_8" type="text" value="42724" style="width:50px" name="U2">
<br>
<input type="button" value="Update" onclick="fnUpdateRevenue("2_8")" style="width:60px">
</div> 
</td>
<td>
<div>
<input id="2_9" type="text" value="22180" style="width:50px" name="U2">
<br>
 <input type="button" value="Update" onclick="fnUpdateRevenue("2_9")" style="width:60px">
</div>
</td> 
<td>
<div>
<input id="2_10" type="text" value="27663" style="width:50px" name="U2">
<br>
<input type="button" value="Update" onclick="fnUpdateRevenue("2_10")" style="width:60px">
</div>
</td>
<td>
<div>
<input id="2_11" type="text" value="88324" style="width:50px" name="U2">
<br>
<input type="button" value="Update" onclick="fnUpdateRevenue("2_11")" style="width:60px">
</div>
</td>
<td>
<div>
<input id="2_12" type="text" value="42853" style="width:50px" name="U2">
<br>
<input type="button" value="Update" onclick="fnUpdateRevenue("2_12")" style="width:60px">
 </div>
</td>
<td>
 <a id="Edit_2" href="#" onclick="fnEditRevenue("2");" style="visibility: hidden;">Edit</a>
|
 <a id="Hide_2" href="#" onclick="fnHideRevenue(2);" style="visibility: hidden;">Hide</a>
|
 <a id="Show_2" href="#" style="visibility:hidden" onclick="fnShowRevenue(2);">Show</a>
</td>
</tr>

我无法在特定月份的列中选择文本框。

//td[position() = count(ancestor::table/tbody/tr/td[.='May']/preceding-sibling::*)+1]

通过这个XPath,我可以选择月份名称“可能&#39;以及所有tr s。但我想使用月份名称仅选择目标列的列和特定输入文本框。

1 个答案:

答案 0 :(得分:1)

我猜你的问题有点不清楚。但是,我认为要求是使用月份名称查找列和关联的td。如果是这样,试试这个:

//td[count(//td//a[contains(.,'October')]//.)]

修改

因为,您只希望inputs type='text'添加额外的过滤功能;

//td[count(//td//a[contains(.,'October')]//.)]//input[@type='text']