Selenium - Returns the Xpath of a clicked item

I am developing Selenium tests in java and I am looking for a solution to my problem. I want to click on the zone (coordinates) of an element during a test. This click implicitly returns me the Xpath of the clicked element present in this zone to compare this Xpath a second time.

Why do I want to compare this Xpath?

I want to check if this Xpath is one of the elements that should be displayed on the screen. For example, when I change the css of my web page, the button may be hidden by a div or wathever. Therefore, I want my test to click on the zone of this button, get the Xpath of the element present in this zone, and check if it is the Xpath of my button!

I already found a solution to click on certain coordinates, but it always depends on the element.

I already developed this solution in Javascript using this:

 if (driver instanceof JavascriptExecutor) {
              ((JavascriptExecutor)driver).executeScript("MY SCRIPT");
          } else {
              throw new IllegalStateException("This driver does not support JavaScript!");
          }

I can integrate the script into my Selenium Java test, but I cannot find a way to get the Xpath return value in the Java variable.

Thanks for helping the guys!

============= SOLUTION =================

I solved my problem. For those who have the same problem, here is my solution: In my Selenium Test in Java, I created a method that includes Javascript:

    public void IsHiddenByOtherElement(WebDriver driver, String xpath, int CordX, int CordY) throws IOException
      {  

              if (driver instanceof JavascriptExecutor) 
              {
                        String script = "setTimeout(function() "
                        + "{jQuery(document.elementFromPoint("+CordX+", "+CordY+")).click();}, 0.1);"
                        + "elementClicked = document.elementFromPoint("+CordX+", "+CordY+");"
                        + "var value2 = "+xpath+";"
                        + "var id = elementClicked.id;$( '#' + id ).click(function() "
                        + "{ "
                        + "var value=xpath(this); "
                        + "if (value!=value2) throw new Error(value + ' : Element cache sous un autre : '+ value2);"
                        + "});"
                        + "function xpath(el) "
                        + "{"
                        + "if (typeof el == 'string') return document.evaluate(el, document, null, 0, null);"
                        + "if (!el || el.nodeType != 1) return '';"
                        + "if (el.id) return '//*[@id=' + el.id + ']';var sames = [].filter.call(el.parentNode.children, "
                        + "function (x) { "
                        + "return x.tagName == el.tagName });"
                        + "return xpath(el.parentNode) + '/' + el.tagName.toLowerCase() + (sames.length > 1 ? '['+([].indexOf.call(sames, el)+1)+']' : '');"
                        + "}";

              ((JavascriptExecutor) driver).executeScript(script); 

              } 
              else 
              {
                  throw new IllegalStateException("This driver does not support JavaScript!");
              }
              final List jsErrors = JavaScriptError.readErrors(driver);
              assertTrue(jsErrors.toString(), jsErrors.isEmpty());
      }

This method simulates a click in certain coordinates, restores the Xpath of this element and compares it with the Xpath of the element that should be displayed on the screen. If the comparison fails, it generates JavaScript errors. This method is also configured to listen for these JavaScript errors and stop the test if it occurs, and return it like any classic Selenium Java errors

script - Javascript.

:

       final List jsErrors = JavaScriptError.readErrors(driver);
       assertTrue(jsErrors.toString(), jsErrors.isEmpty());

, JavaScript , .

,

       FirefoxProfile ffProfile = new FirefoxProfile();
       JavaScriptError.addExtension(ffProfile);
       final WebDriver driver = new FirefoxDriver(ffProfile);
       driver.get("file:///Users/Max/Downloads/elementfrompoint.html");
       IsHidden(driver,"'//*[@id=test]'",50,50);

!

+4

All Articles