0

Suppose I have stored WebElment in LoginPage.java class as shown below.

@FindBy(xpath="//input[@name='user_name']") WebElement username;

Now from my test file say VerifyLogin.java I want to access xpath of username which is stored in above LoginPage.java file something like this

LoginPage loginPage = PageFactory.initElements(driver, LoginPage.class);
loginPage.username;//then it should return the xpath //input[@name='user_name']

I know the above approach is wrong, but is there any way to get only the xpath that was stored in LoginPage file that I can use in VerifyLogin file?

Shoaib Akhtar
  • 1,393
  • 5
  • 17
  • 46
  • 1
    You can find helpful information [here](https://stackoverflow.com/questions/4296910/is-it-possible-to-read-the-value-of-a-annotation-in-java) – Sers Feb 22 '19 at 14:45
  • I'm curious why you need the xpath itself, but to answer the question I would probably just define a String variable in LoginPage that contains the xPath and use that. – C. Peck Feb 22 '19 at 14:33
  • Yes I am aware if xpath is stored as String variable then I can achieve it, then in this case I will miss the page object model feature as I am storing all web element using @FindBy and creating method based on pages. So I don't want change it. – Shoaib Akhtar Feb 22 '19 at 14:40

3 Answers3

1

If you need it outside of the page object then your design is not that good .

Anyhow if you really want it the following should do the job

@FindBy(xpath="//input[@name='user_name']") 
public WebElement username;

new YourPageObject().username;

teddym6
  • 88
  • 9
1

Not sure why you need to see the xpath after it's done its job but you can do something like this:

public final String xUserName = "//input[@name='user_name']";
@FindBy(xpath = xUserName)
public WebElement username;

The final attribute is required.

This way you store the path as a string, and use the string in your POM, but it is still readable as a string. I start the String variable with x so I know that the String is in xpath format. Start with c for CSS Selector notation.

I've done similar for a couple reasons:

  1. The actual xpath is gigantic so I break it into smaller, reusable String chunks. But for that I mark the String as private.
  2. I need the string to pass to other Selenium methods such as a pause class I created.

But to answer the direct question, no, you can't get the xpath from a POM variable because that information is not stored. The variable is a WebElement and doesn't care by what method you assigned it. It's like asking how a String was created.

MivaScott
  • 1,763
  • 1
  • 12
  • 30
0

you can use this small trick , I used it and it worked for me.

toString method lets you print webelement which has its xpath in it then using substring method get ur xpath

// method to fetch xpath from PageFacotry webElement

public String getXpathFromWebelement(WebElement e) {   
        String xpath = e.toString();
        xpath = xpath.substring(xpath.indexOf("/"), xpath.lastIndexOf("]"));
        return xpath;
    }