Can we use css properties (like background-color) for locating an element when that property is not part of the element attributes? Like enter image description here

If I have to get the xpath or css of this element, either I have to use class or href attributes or similar way. But I want to find it based on the background color, which is not shown anywhere in the attributes but it is seen in the element style.

Please answer if this is possible

You can reference any combination that renders in the DOM. So in this case the css class and the element type you can reference. As it's a link that likely has multiple associations to the class I would utilize an xquery that includes the parent object and then the a link and class, or the rendered text of the link if that is unique.

In general you can't used derived class color information, but only what is rendered in the DOM. The drilled in view of the DOM that you have is what you can utilize to locate elements, if it isn't in there then you can't use it as the screen reader is literally reading the DOM.

xquery is really powerful and I would recommend learning that for your element attachment and locating. Good xquery example page

  • Thanks for your answer. So Anything that is not seen in the DOM can't be used to find the element, is that right? – Soma Shekhar P Jul 12 at 15:55
  • Yes, you can utilize JavaScript to manipulate the DOM but the screen reader will only read the rendered DOM. – mutt Jul 13 at 6:30

You can only locate element by style if the element has style attribute inline so that you will be able to use that attribute like you normally do when use attributes in css selectors.

However you cannot use computed styles to locate element using css selectors. See this post for details: https://stackoverflow.com/questions/8426882/css-selector-by-inline-style-attribute

Basically css-selectors are originally used to define a scope of elements you want to apply the styles to, not to defined the scope of elements having the particular style.

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.