Selenium:我如何断言某个表格的特定单元格中存在某个元素? [英] Selenium: How do I assert that a certain element is present in a certain cell of a certain table?
问题描述
我的页面上有一个表,它应该包含某个元素。我可以通过名称识别表(它有一个唯一的名称),我也可以轻松识别该元素。我想断言该元素出现在表的行 r
,列 c
中。使用Selenium命令最干净的方法是什么?
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?
备注:
- 我不想使用多个表名来找到它(我不想要所有
div \div \ table \ diviv代码中的\\ ttant\td\ [r] \td [c]
。 - 我在PHPUnit中使用Selenium。因此,我可以使用PHP逻辑来完成任务,但我不希望任何复杂的逻辑用于这么简单的任务。
澄清:
如果单元格中的元素只是纯文本,那么我可以像这样检索该文本:
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)
但是如果单元格的元素不仅仅是纯文本怎么办?如果元素是链接( link = anchor
)或按钮( //按钮[@type ='button'] $ c,该怎么办$ c>)或图像或更复杂的东西?
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.
推荐答案
听起来像你想要 isElementPresent(...元素的定位器... )
。例如:
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()
返回 true
如果是, false
如果不是。
isElementPresent()
returns true
if so, false
if not.
这篇关于Selenium:我如何断言某个表格的特定单元格中存在某个元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!