Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 380
user1563042 Avatar asked Jul 30 '12 14:07

user1563042


People also ask

Does Selenium support custom attributes?

Yes, you can use both CSS and XPATH selectors to find an element bya custom attribute.

How do I find an element that contains specific text in Selenium WebDriver?

We can find an element that contains specific text with Selenium webdriver in Python using the xpath. This locator has functions that help to verify a specific text contained within an element. The function text() in xpath is used to locate a webelement depending on the text visible on the page.


1 Answers

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']")) 
like image 170
Justin Ko Avatar answered Sep 25 '22 12:09

Justin Ko