0

I'm using Selenium Webdriver in Java. I have a table, and I like to get my hands on the last cell on the first row, and the last cell of last row. I manage to get one of them

WebElement table =driver.findElement(By.className("dataTable"));
List <WebElement> rows = table.findElements(By.tagName("tr"));
WebElement firstrow= rows.get(0);
WebElement lastrow= rows.get(rivit.size()-1); 
List <WebElement> firstcells = firstrow.findElements(By.tagName("td"));
List <WebElement> lastcells = lastcell.findElements(By.tagName("td"));

firstcell.get(6).getText());

This is because I'm locating td-tags twice. Any hints how to get both cells nicely? I have no identifiers in my rows or cells.

1
  • It might help to include your actual source here... for example: what is lastcell assigned to? Did you just mean instead lastcells = lastrow.findElements(By.tagName("td"));? Commented Sep 12, 2012 at 13:36

3 Answers 3

1

You can use xpath to get the elements:

WebElement lastCellInFirstRow = driver.findElement(By.xpath("table[@class='dataTable']//tr[1]//td[last()]"));
WebElement lastCellInLastRow = driver.findElement(By.xpath("table[@class='dataTable']//tr[last()]//td[last()]"));

Here's the xpath specification. You can play with xpath here.

Sign up to request clarification or add additional context in comments.

1 Comment

Is there a more specific error message? Could you try again but start the expression with //table instead of table? I don't use selenium server myself so I can't try.
0

You can try to make it with cssSelectors:

String cssLast="table[class='dataTable']>tr:first-child>td:last-child"
String cssFirst="table[class='dataTable']>tr:last-child>td:last-child"

it will be smt like that;

driver.findElement(By.cssSelector(cssLast)).getText();
driver.findElement(By.cssSelector(cssFirst)).getText();

another approach is using js:

String getText(cssSel){
 JavascriptExecutor js = (JavascriptExecutor) driver;
        StringBuilder stringBuilder = new StringBuilder();

stringBuilder.append("var x = $(\""+cssSel+"\");");
        stringBuilder.append("return x.text().toString();")       ;


       String res= (String) js.executeScript(stringBuilder.toString());
}

text1=getText(cssLast);
text2=getText(csscssFirst);

But always make sure that you located elements properly (e.g. using firepath, firebug addon in firefox) enter image description here

Comments

0

The TableDriver extension (https://github.com/jkindwall/TableDriver.Java) offers a nice clean way to handle things like this. If your table has headers, you can (and should) identify the cell column by its header text, but in case you don't have headers, you can still do something like this.

Table table = Table.createWithNoHeaders(driver.findElement(By.className("dataTable")), 0);
WebElement firstRowLastCell = table.findCell(0, table.getColumnCount() - 1);
WebElement lastRowFirstCell = table.findCell(table.getRowCount() - 1, table.getColumnCount() - 1);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.