Selenium: How do I assert that a certain element is present in a certain cell of a certain table?

和自甴很熟 提交于 2019-12-01 16:28:26

问题


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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/7240402/selenium-how-do-i-assert-that-a-certain-element-is-present-in-a-certain-cell-of

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!