-7

This is the HTML I want to extract from.

<input type="hidden" id="continueTo" name="continueTo" value="/oauth2/authz/?response_type=code&client_id=localoauth20&redirect_uri=http%3A%2F%2Fbg-sip-activemq%3A8080%2Fjbpm-console%2Foauth20Callback&state=72a37ba7-2033-47f4-8e7e-69a207406dfb" />

I need a Xpath to extract only state value i.e 72a37ba7-2033-47f4-8e7e-69a207406dfb

Aminah Nuraini
  • 18,120
  • 8
  • 90
  • 108
Saagar
  • 794
  • 3
  • 20
  • 41
  • Post your efforts so far (your current code/regex) so we can help you better. – dquijada Apr 19 '16 at 07:48
  • 1
    Just a friendly tip, you may want to read over this page: [The How-To-Ask Guide](https://stackoverflow.com/help/how-to-ask) so you can always be sure that your questions are easily answerable and as clear as possible. Be sure to include any efforts you've made to fix the problem you're having, and what happened when you attempted those fixes. Also don't forget to your show code and any error messages! – Matt C Apr 19 '16 at 08:02
  • You might want to also have a look at [*XSLT: Finding last occurance in a string*](http://stackoverflow.com/questions/3141847/xslt-finding-last-occurance-in-a-string). – Wiktor Stribiżew Apr 19 '16 at 08:03

3 Answers3

2

The XPATH:

//input[@id="continueTo"]/@value

It will get the value of the node input with id continueTo. Then it will need to be processed with a Regex first to get a final result.

The Regex:

`[^=]+$`

$ means the end of the string. It will get everything on the end of the string which is not =.

Aminah Nuraini
  • 18,120
  • 8
  • 90
  • 108
  • But I dont see it on xpath, Can you please post complete xpath, If Xpath has all the logic having stringoperations over attributes it would be good Like split of string, extracting some second value etc – Syed Rafi Jun 30 '21 at 15:28
2

Hi you can only get value of a attribute using xpath not its sub-string but if you want to get sub-string then please do it like below

String AttributeValue = driver.findElement(By.id("continueTo")).getAttribute("value");
System.out.println("Value of the attribute value is : " + AttributeValue);

    // now as you want 72a37ba7-2033-47f4-8e7e-69a207406dfb this substring of the vale attribute 
    // then plz apply java split as below 
    String value = "/oauth2/authz/?response_type=code&client_id=localoauth20&redirect_uri=http%3A%2F%2Fbg-sip-activemq%3A8080%2Fjbpm-console%2Foauth20Callback&state=72a37ba7-2033-47f4-8e7e-69a207406dfb";
    String [] myValue = value.split("=");
    System.out.println(myValue[0]); // will print /oauth2/authz/?response_type
    System.out.println(myValue[1]); // will print code&client_id
    System.out.println(myValue[2]); // will print localoauth20&redirect_uri
    System.out.println(myValue[3]); // will print http%3A%2F%2Fbg-sip-activemq%3A8080%2Fjbpm-console%2Foauth20Callback&state
    System.out.println(myValue[4]); // will print 72a37ba7-2033-47f4-8e7e-69a207406dfb

Hope this helps your query

Rajnish Kumar
  • 2,828
  • 5
  • 25
  • 39
1

Use below code:-

    String val = driver.findElement(By.xpath("//input[@id='continueTo']/@value")).getText();

    String [] myValue = val.split("&state=");

    System.out.println(myValue[1]);

Hope it will help you :)

Shubham Jain
  • 16,610
  • 15
  • 78
  • 125