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?

1

1 Answer 1

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?

Sign up to request clarification or add additional context in comments.

3 Comments

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.
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']
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.