0

I am creating an application using java swing for verification of Object repository which would help maintain selenium scripts. This app would launch a WebDriver instance and then let the user navigate manually to the element which he/she wishes to inspect.

I want to get instance of the WebElement object which the user clicks so that i can pass the last clicked web element as the argument within below function which returns an Xpath.

I couldn't find anything related to this in web.

public String getElementXPath(WebDriver driver, WebElement element) {
    return (String)((JavascriptExecutor)driver).executeScript("gPt=function(c){if(c.id!==''){return'id(\"'+c.id+'\")'}if(c===document.body){return c.tagName}var a=0;var e=c.parentNode.childNodes;for(var b=0;b<e.length;b++){var d=e[b];if(d===c){return gPt(c.parentNode)+'/'+c.tagName+'['+(a+1)+']'}if(d.nodeType===1&&d.tagName===c.tagName){a++}}};return gPt(arguments[0]).toLowerCase();", element);
}
soundslikeodd
  • 1,078
  • 3
  • 19
  • 32

2 Answers2

0

You cannot get instance just by clicking on element. You should make unique xpath by yourself and then you can.

To get instance of webelement you can use next:

WebElement el = (WebElement)((JavascriptExecutor)driver)
                              .executeScript("document.evaluate(xpath, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;");

In that case you should build xpath by yourself and then find element using it. See here and here

nick318
  • 575
  • 4
  • 18
  • @julian or Anyone else, Could you please help me on how to inject JS into the browser that will create a hidden element which will have the attribute attached to it refreshed everytime there is a mouse click event ? Appreciate your help. Thanks in Advance!! – Siddharth Mishra Feb 16 '18 at 17:05
0

There isn't a solution boxed up by webdriver for this problem. What you'll need to figure out is how to generate a locator such as xpath or css selector based on what you click. This very likely will need to be in the form of some injected javascript on the page that listens for clicks. Quick google search yielded this answer How to get the xpath by clicking an html element

Once you have that locator, your next task will be to get it brought into scope of your webdriver code, so that you may use it's value in your test.

Just off the top of my head, one way you could do this is to use that the same javascript injection to create a hidden element on the page off in a corner that has it's value attribute updated on each click to the output of whatever you decide will generate your locator. This will allow your java code to fetch that secret element and ask it for the xpath to the previously clicked element.

One caveat though, if you were to inject anything on the page, make sure that your injections don't change the context of the elements on the page. If you inject anything that has a functional impact on real page elements, you may put yourself into a place where your xpaths stop working the moment you restore the page to it's natural state (through a page refresh or whatever).

Generally speaking, injections into the page are bad for testing because it invalidates the known state of the page. However, it sounds like you're trying to accomplish an xpath aid tool for assisting in writing webdriver code in another context, which could be fine as long as your xpaths are properly generated, and injections aren't changing their absolute locations.

One more thing to think about, are you hoping for locators to be generated that are concise and well built? Any generated locator you find will likely be the result of crawling the dom and building a long-winded absolute path, which will be broken much much easier by an actively developed site.

Julian
  • 1,665
  • 2
  • 15
  • 33