无法使用java在selenium webdriver中打印特定名称

时间:2015-06-11 10:27:48

标签: java html selenium

我的Java

package com.ej.zob.modules;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

public class Manage_Branch {
public void Execute(String IFSC)
{
    LaunchApplication.driver.findElement(By.linkText("MANAGE")).click();
    LaunchApplication.driver.findElement(By.linkText("BRANCH")).click();
    LaunchApplication.driver.findElement(By.xpath("//tbody/tr/td/div[text()='"+IFSC+"']"));
    WebElement col = LaunchApplication.driver.findElement(By.xpath("//table/tbody/tr/td[count(//table/thead/tr/th[.='BRANCH NAME']/preceding-sibling::th)+4]"));

    if(IFSC.equalsIgnoreCase(IFSC))
    {
        System.out.println(col.getText());

    }
}

}

我的HTML

<table id="flex1" cellspacing="0" cellpadding="0" border="0">
<thead>
<tr class="hDiv">
<th width="6%">
<div class="text-left field-sorting asc" rel="IFSC_CODE"> IFSC CODE </div>
</th>
<th width="6%">
<div class="text-left field-sorting " rel="BRANCH_NAME"> BRANCH NAME </div>
</th>
</tr>
</thead>
<tbody>
<tr>
<td class="sorted" width="6%">
<div class="text-left">SACS011151</div>
</td>
<td width="6%">
<div class="text-left">check</div>
</td>
</tr>
<tr class="erow">
<td class="sorted" width="6%">
<div class="text-left">SACS011152</div>
</td>
<td width="6%">
<div class="text-left">Motiram</div>
</td>
</tr>
<tr class="erow">
<td class="sorted" width="6%">
<div class="text-left">SACS011158</div>
</td>
<td width="6%">
<div class="text-left">TESTNAME</div>
</td>
</tr>
</tbody>
</table>

我想从列名“BRANCH NAME”打印值说“check”但是当用户通过IFSC CODE的值说“SACS011151”时,那个特定的IFSC CODE前面的值应该打印出来。但在我的情况下它正在打印第8行的值,无论您传递的IFSC代码是什么,只接受“TESTNAME”。如需帮助,您可以查看上面的图片以便更好地理解。我不知道为什么会这样。希望你理解任何帮助?

3 个答案:

答案 0 :(得分:1)

试试这个: WebElement col = LaunchApplication.driver.findElement(By.xpath(&#34; // thead // div [text()=&#39; +&#34; IFSC&#34; +&#39;] //。 ./following-sibling::td/div"));

答案 1 :(得分:0)

WebElement col = LaunchApplication.driver.findElement(By.tagName('tbody')).findElement(By.tagName('tr')).findElements(By.tagName('td'))[1]

答案 2 :(得分:0)

试一试:

   WebElement col = LaunchApplication.driver.findElement(By.xpath("//tbody/tr[1]/td[1]/div[text()='SACS011151']"));

if(col.equals("SACS011151")){
     WebElement branchNameEle = LaunchApplication.driver.findElement(By.xpath("//tbody/tr[1]/td[4]"));
     System.out.println("BranchName: "+branchNameEle.getText());
}