Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Protractor - How to locate element by custom (non HTML) attributes?

I am using Selenium WebDriver and Protractor to run e2e tests on my angular project. Let's say I have an element like:

<div my-directive my-unique-id="abc123"></div>

How can locate the above element. I tried with element(by.css('div[my-unique-id="abc123"]'));, but it gives a NoSuchElementError.

If I try with HTML attributes like, for example, I want to locate:

<a title="myTitle" href="">Click me</a>

I was able to locate the element correctly using element(by.css('a[title="myTitle"]'))

How do I locate the element with custom attributes, if it does not have any standard HTML attributes?

like image 679
Madhura Adawadkar Avatar asked May 13 '14 11:05

Madhura Adawadkar


2 Answers

Try to use xpath:

element(by.xpath('//div[@my-unique-id="abc123"]'))

or only by attribute

element(by.xpath('//div[@my-unique-id]'))
like image 100
Andrian Durlestean Avatar answered Nov 04 '22 04:11

Andrian Durlestean


try using:

element(by.css('[my-unique-id="abc123"]'))

it's easier and more readable than xpath for simple cases.

more about xpath syntax and when it is usefull: http://www.w3schools.com/xml/xml_xpath.asp

like image 5
Sauerkraut Avatar answered Nov 04 '22 06:11

Sauerkraut