0

We are getting url from JSON Response and which we open in in Chrome.The page loads , there is submit button which we click then it redirect to url as :- https://www.google.com/AB1234

We need the need to retrieve only "AB1234" value from url.

tried following code to get value ="AB1234"

String url = driver.getCurrentUrl();
            int index=url.lastIndexOf("/");
            String result = url.substring(0,index);


but here getting initial part of url:https://www.google.com/

Vipin Singh
  • 109
  • 1
  • 14

2 Answers2

2

You need to call substring function with index +1 .

Try below code :

    String url = driver.getCurrentUrl();
    int index = url.lastIndexOf("/");
    String result = url.substring(index + 1);
Joby Wilson Mathews
  • 10,528
  • 6
  • 54
  • 53
0

To parse a URI, it's likely a good idea to use a URI parser.

Given http://example.com/bar

 String path = URI.create(driver.getCurrentUrl()).getPath();

will get you '/bar'.

Given http://example.com/bar/mumble the same code gets '/bar/mumble'. It's unclear from your question whether this is what you want. Nevertheless, you should at least start the parse as above.

a guest
  • 462
  • 3
  • 5