6

I have a table on my page which is supposed to contain a certain element. I can identify the table by its name (it has a unique name), and I can also identify the element easily. I would like to assert that the element is present on row r, column c of the table. What is the cleanest way of doing it using Selenium commands?

Remarks:

  • I don't want to use more than the table name in order to locate it (I don't want all the div\div\table\div\tbody\td\tr[r]\td[c] in the code).
  • I'm using Selenium within PHPUnit. Hence, I can use PHP logic for the task, though I don't want any complex logic for such a simple task.

Clarification:

If the element in the cell is just plain text, then I can retrieve that text like this:

$this->getText("xpath=//table[@name='tableName']//tr[".$r."]//td[".$c."]"); (PHP)

But what if the cell has an element which is not just plain text? What if the element is a link (link=anchor) or a button (//button[@type='button']) or an image or something more complex?

I need to assert that an element specified by a locator of that element resides in a given cell.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
snakile
  • 52,936
  • 62
  • 169
  • 241
  • Could you add an HTML example? – powtac Aug 30 '11 at 10:35
  • `\\tr[r]\td[c]` does not work? – powtac Aug 30 '11 at 10:36
  • @powtac: `\\tr[r]\td[c]` works, but `\\tr[r]\td[c]` just locates the cell. What I need to do is asserting that inside that cell (which I can locate easily with `\\tr[r]\td[c]`) resides a specified element (which I can locate using its id or something). See the clarification I've added to the question. – snakile Aug 30 '11 at 12:09

2 Answers2

0

You could try Selenium's getXpathCount

$this->("xpath=//table[@name='tableName']//tr[".$r."]//td[".$c."]//TAG");
This will return the number of matches the xpath gets. in your case, zero would mean a fail.

Raidil142
  • 137
  • 1
  • 10
0

Sounds like you want isElementPresent(...locator of element...). For example:

$cell = "//table[@name='tableName']//tr[".$r."]/td[".$c."]";
$foundLink = $this->isElementPresent("xpath=".$cell."/a[.='".linktext."']");
$foundButton = $this->isElementPresent("xpath=".$cell."/button[@type='button']");
$foundImage = $this->isElementPresent("xpath=".$cell."/img[ends-with(@src='pretty-pony.gif')]");

isElementPresent() returns true if so, false if not.

Ross Patterson
  • 9,527
  • 33
  • 48