Selenium: How to say that a certain element is present in a certain cell of a certain table?

I have a table on my page that should contain a specific element. I can identify a table by its name (it has a unique name), and I can also easily identify an element. I would like to claim that the element is present in row r , column c table. What is the cleanest way to do this with Selenium commands?

Notes:

  • I don’t want to use more than the table name to find it (I don’t want all div\div\table\div\tbody\td\tr[r]\td[c] in the code).
  • I am using Selenium in PHPUnit. Therefore, I can use PHP logic for this task, although I do not need complex logic for such a simple task.

Clarification:

If the element in the cell is just text, then I can get this text as follows:

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

But what if a cell has an element that is not just 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 claim that the element indicated by the locator of this element is in this cell.

+6
source share
2 answers

It looks like you want isElementPresent(...locator of element...) . For instance:

 $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.

0
source

You can try Selenium getXpathCount

$ this is "[.. $ C"]> ("= // XPath table [@ name = 'TableName'] // tr [" .. $ G] // td "// TAG ");
This will return the number of hits that xpath receives. in your case, zero will mean failure.

0
source

Source: https://habr.com/ru/post/896188/


All Articles