user1563042
user1563042

Reputation: 261

Selenium webdriver : how to find the element in DOM based on custom attribute

How can I find the element in DOM based on a custom attribute?

For example:

DOM attributes are not present in HTML view. Using DOM inspector I can able to identified the Custom attribute is unique.

driver.findElement(By.id("SimpleSearch:dIndicesGrid:1:Value")).getAttribute("_celltype");

Here _celltype is custom attribute. This attribute is not visible in HTML view.

Upvotes: 26

Views: 56609

Answers (3)

Matas Vaitkevicius
Matas Vaitkevicius

Reputation: 61391

XPath is evil, you could use this instead

By.CssSelector("[_celltype='celltype']");

Upvotes: 24

Prabu Ananthakrishnan
Prabu Ananthakrishnan

Reputation: 4249

Find the element by xPath:

WebElement element = driver.findElement(By.xpath("xpath_link"));

xpath_link = //*[@_celltype='celltype']; // This is sample xpath;

System.out.println(element.getText());

This will get the text of 'celltype' field and displays the value of it.

Upvotes: 2

Justin Ko
Justin Ko

Reputation: 46836

You would have to locate the element by xpath.

The following would find any element that has the _celltype attribute with value 'celltype':

driver.findElement(By.xpath("//*[@_celltype='celltype']"))

If you know what type of element it is you can make it more specific. For example, if you know they are div tags, do:

driver.findElement(By.xpath("//div[@_celltype='celltype']"))

Upvotes: 19

Related Questions