0

I am trying to get the XPath and CSS path of an element using Java. I have used jsoup to parse the HTML and I am getting the CSS path, but in some cases it is returning the wrong path. (I am matching it with Selenium generated paths.)

I am using this code to generate CSS path my element is "s-Rectangle_44"

<div id="s-Rectangle_44" class="rectangle firer click commentable">
  <div class="clipping">
    <div class="content">
      <div class="valign">
        <span id="rtr-s-Rectangle_44_0"></span>
      </div>
    </div>
  </div>
</div>

And selenium is giving css path as css=#s-Rectangle_44 > div.clipping > div.content > div.valign while I am getting array Index out of bound exception.I need to get the XPath also. Is there any other method to get this? Can I use Firebug with Java?

public static String getCssPath(Element el) {
  if (el == null)
    return "";

  if (!el.id().isEmpty())
    return "#" + el.id();

  StringBuilder selector = new StringBuilder(el.tagName());
  String classes = StringUtil.join(el.classNames(), ".");
  if (!classes.isEmpty())
    selector.append('.').append(classes);

  if (el.parent() == null)
    return selector.toString();

  selector.insert(0, " > ");
  if (el.parent().select(selector.toString()).size() > 1) {
    selector.append(String.format(":nth-child(%d)",
        el.elementSiblingIndex() + 1));
  }

  return getCssPath(el.parent()) + selector.toString();
}
Sebastian Zartner
  • 18,808
  • 10
  • 90
  • 132
user3649361
  • 944
  • 4
  • 20
  • 40
  • Yes you can use Xpath and also firebug too. – Rupesh Shinde Nov 28 '14 at 10:11
  • Please edit your question and add an example for a wrong CSS path you got and what you expected to get. Firebug cannot be connected with Java. – Sebastian Zartner Nov 29 '14 at 13:22
  • possible duplicate of [How to read XML using XPath in Java](http://stackoverflow.com/questions/2811001/how-to-read-xml-using-xpath-in-java) – Sebastian Zartner Nov 29 '14 at 13:23
  • @SebastianZartner I don't want to read XML using XPath.I don't have XPath, I have an id of an element now I have to parse the html file and find the XPath and CSSPath of that element. – user3649361 Dec 01 '14 at 07:29
  • Sorry, I misread the intention. Anyway, please post more info on what you've tried so far. See also my first comment. – Sebastian Zartner Dec 01 '14 at 08:12
  • While I provided a general answer how to get the XPath and CSS path for an element, it's still unclear, for which element(s) you want to get the paths and what path(s) you actually expect. – Sebastian Zartner Dec 14 '14 at 23:39

2 Answers2

1

CSS path

According to the jsoup API reference an Element has a cssSelector() function, which returns the related CSS path.

This function simply returns #s-Rectangle_44 for your element with the ID s-Rectangle_44.

XPath

If you can assume that every element you want to match has an ID, then this code may be sufficient:

String getXPath(Element el)
{
  return "//*[@id='" + el.id() + "']";
}

Regarding your example this would return //*[@id='s-Rectangle_44'] for your element with the ID s-Rectangle_44.

There is also an answer to a similar thread, which has code for absolute paths.


Firebug's code (written in JavaScript) has functions for getting the CSS path and functions for getting the XPath, which generate absolute paths and could be translated to Java.

Note that Firebug is under BSD license.

Community
  • 1
  • 1
Sebastian Zartner
  • 18,808
  • 10
  • 90
  • 132
  • thanks for the answer. Can you tell me what is the difference between Xpath generated by Firebug and the Xpath generated by selenium and which one should be preferred. – user3649361 Dec 15 '14 at 09:57
  • Firebug returns an absolute XPath, while Selenium generates relative ones. There is no general answer for which one is better. It depends on your requirements. I suggest you to read the [XPath specification](http://www.w3.org/TR/xpath/#path-abbrev) for further information about its syntax. With that knowledge you should be able to decide what fits your needs. – Sebastian Zartner Dec 15 '14 at 10:30
  • Do you mean a way to implement the source code of Selenium IDE into your Java environment? – Sebastian Zartner Dec 16 '14 at 06:52
  • yes i need the basic functionality of selenium IDE like command , target value through the code – user3649361 Dec 16 '14 at 07:07
  • Selenium IDE is written in JavaScript like all Firefox extensions. The [code can be found on the Google Code page of Selenium](https://code.google.com/p/selenium/source/browse/ide/main/src/content/locatorBuilders.js?r=967286a7dc4acae58e95d53435c272a69b115d87#255). The code is under the Apache License 2.0. – Sebastian Zartner Dec 16 '14 at 08:08
  • Thanks, I am not able to figure out how to use it is there any documentation available for this code. What i need to do is that I have an id of an element like "rtr-s-Rectangle_44_0" in above example and I need to get the command, target value for that element(id). – user3649361 Dec 17 '14 at 04:59
  • There's a [wiki](https://code.google.com/p/selenium/w/list) available for Selenium, though I fear there's no documentation of that specific methods. So you should [ask the team behind Selenium](https://groups.google.com/forum/#!forum/selenium-developers) to get to know how their code works. – Sebastian Zartner Dec 17 '14 at 07:37
-2

I would recommend to make your own XPath manually, because sometimes you need to use other attributes than "id" and classes like e.g. "data-id" when a developer uses it to store some properties.

Another way to get an XPath for an element is via the developer panel in Chrome. Just right-click the element and click Copy XPath. But it copies an absolute XPath with ID.

Sebastian Zartner
  • 18,808
  • 10
  • 90
  • 132
Bilal Naqvi
  • 140
  • 1
  • 11