0

I'm trying to create a dynamic "element map" of the elements displayed inside a banner menu including the link displayed for each menu. This map will be stored inside a String[][] matrix, which will be used for different methods and tests.

To do that I get the element's href attribute and create my own CSS selector for each link displayed in the menus. This is my the code to create the CSS:

CSS = (String)element.getAttribute("href").subSequence((element.getAttribute("href").length()-20), element.getAttribute("href").length());

CSS="a[href*='" + CSS +"']";

That works pretty well, but I have some links that run some JavaScript code and I cannot create the CSS selector for them.

Is there any onther way to get the CSS selector from a WebElement? Using Firebug I can get the CSS path. Could I get this CSS path during the execution having the WebElement? Any other suggestions?

Sebastian Zartner
  • 18,808
  • 10
  • 90
  • 132

1 Answers1

0

The answer is No. You cannot extract a CSS selector from a WebElement.

Why? Because the WebElements themselves are found by the By class.. What if the By specified was xpath? How would it populate the CSS selector?

ddavison
  • 28,221
  • 15
  • 85
  • 110
  • Hi, thanks for our answer. Ok, I can not get a CSS selector, but, do you know, how I could create a CSS selector for a Link which is calling a js? I would want to add this identifier into a string matrix to use them later and avoid hoardcoding them..., any suggestion?, thanks you very much again. – Miguel Batuecas Nov 08 '13 at 11:32
  • Yea, you can. If your link is using `onclick`, then just use `a[onclick*='whatever']` If it's using something like href, do the same thing. `a[href*='whatever']` – ddavison Nov 09 '13 at 18:28
  • Hi, thanks for your reply, unfortunately the link does not use onclick and I can not use href becuase it is calling a js, I think, I'm going to change the way to store the elements, instead create a String matrix with the CSS selector for each element, I'm going to create a WebEleemt matrix with the links and ask for their properties when I need it, it should work in the same way and solve the current problem. thanks again! Miguel. – Miguel Batuecas Nov 11 '13 at 11:25