2

I am using page object model. Here is XPath that I have written for Heading of the page and stored it in the heading WebElement variable:

@FindBy( xpath = "//div[@class='page-title category-title']")
private WebElement heading;`

My goal is to get or extract the XPath back from the element heading. I have tried printing it but the result I am getting is, System.out.println(heading)

gave me:

[[FirefoxDriver: firefox on WINDOWS (4520a9a1-c58b-4b34-ae3e-48613e6e7005)] -> xpath: //div[@class='page-title category-title']]`

And my expectation is :

//div[@class='page-title category-title'] 

as a string.

5 Answers 5

1

Just extract the class attribute of the element and create the xpath

String headingClass = heading.getAttribute("class");

String headingPath = "//div[@class='"+ headingClass + "']"

then use headingPath wherever you want.

1
String source = "[[FirefoxDriver: firefox on WINDOWS (4520a9a1-c58b-4b34-ae3e-48613e6e7005)] -> xpath: //div[@class='page-title category-title']]";
System.out.println((source.substring(0, source.length() - 1).split("-> ")[1]));
//Output:
xpath: //div[@class='page-title category-title']
  1. (source.substring(0, source.length() - 1) : this will remove the last element which is ']'(the last square bracket)

  2. split("-> ")[1] : will split the string/element and return it's 1 element.

System.out.println(source.split("-> ")[1]);

Output: xpath: //div[@class='page-title category-title']]

do you see the last ']'? that's the one I'm removing in the first step.

3
  • Welcome to the community! It's helpful to NOT just write code but add an explanation. You say this improves a previous answer..how? Why is this a better solution? It's recommended to review this: sqa.stackexchange.com/help/how-to-answer
    – Lee Jensen
    Commented Feb 10, 2022 at 19:02
  • adding one more split function " split("xpath:")[1]" at the end will remove the string "xpath:" output : //div[@class='page-title category-title'] Commented Jul 20, 2022 at 9:21
  • Thanks @SadhaNanda. I didn't do that because, I need that string as well incase if it's id, name, etc other than xpath. Commented Jul 21, 2022 at 10:10
0

I don't know why you want the output like this , but i can think of one way of getting you the class name instead of the whole xpath,

System.out.println(heading.getAttribute("class"));

which will give you result as page-title category-title

Then you can prefix & postfix with you xpath string if only you want the output like that, then again as i said, i don't know why you need output string like that,

System.out.println("//div[@class='"+heading.getAttribute("class")+"']");

Want to know out of curiosity, whats the purpose of doing this?

1
  • 1
    Why? In my experience, because ExpectedConditions only work with locators and not with existing webelements... It's kind of silly to redefine an XPath if you already know the element.
    – FDM
    Commented Nov 9, 2015 at 19:31
0

We will be splitting the string with the delimitor "xpath:"

String[] a = heading.toString().split("xpath:");

//The length will be 2
System.out.println("length"+a.length); 

// will print //div[@class='page-title category-title']
System.out.println(a[a.length-1]); 
-1
String source = "[[FirefoxDriver: firefox on WINDOWS (4520a9a1-c58b-4b34-ae3e-48613e6e7005)] -> xpath: //div[@class='page-title category-title']]";

//Split your Source from "->"
String[] temp = source.split("-> ");

//Split your Source from ": "
String[] temp2 = temp[1].split(": ");

//Access first index to get locator name
String locator = temp2[0];

//Access Second index to get locator value
String value = temp2[1].substring(0,temp2[1].length()-1);
1
  • This really doesn't add anything to the existing answers to a several-year-old question, and is actually less efficient with the multiple split operations, for no apparent added value.
    – c32hedge
    Commented Sep 9, 2019 at 16:40

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.