Monday, May 18, 2015

How to click checkbox/link from a WebTable where Table id is unknown in a web page


//Select Link Object
List<WebElement> MainTables = driver.findElements(By.tagName("table"));

int i_TblCount = 0;
String strProjectName = "TestAutomation6";
String strActualProjName;
for (WebElement TblPosition:MainTables)
{
String strText;
strText = TblPosition.getText().trim();

if(strText.startsWith("+ Restructure Opportunities"))
{

System.out.println(strText);
System.out.println(" | ");
List<WebElement> tblPosition1 = TblPosition.findElements(By.tagName("table"));
System.out.println("Tables within Restructure Opp" + tblPosition1.get(1).getText());

List<WebElement> trows = tblPosition1.get(1).findElements(By.tagName("tr"));
for( WebElement TblRow:trows)
{
System.out.println(TblRow.getText());
List<WebElement> tds = TblRow.findElements(By.tagName("td"));
if(tds.size()>2){
strActualProjName = tds.get(1).getText();
System.out.println(strActualProjName);
if(strProjectName.equals(strActualProjName))
  {
//tds.get(0).click();
  //String strtdtext = tds.get(0).findElement(By.tagName("input")).getAttribute("innerhtml");
  //CheckBox
  tds.get(0).findElement(By.tagName("input")).click();
  //tds.get(1).findElement(By.tagName("a")).click();
  String s = tds.get(1).findElement(By.tagName("a")).getText();
  tds.get(1).findElement(By.tagName("a")).click();
  //tds.get(1).findElement(By.linkText(strActualProjName)).click();
  System.out.println(s);
  Thread.sleep(4000);
  driver.findElement(By.linkText("Campaigns")).click();
  Thread.sleep(4000);
  break;
} //If Project Name found in the List

 }//if tds.size

} //For to find Project List Table

}// If table starts with Restructure Opportunities

}//All Table For Loop

No comments: