2

My page URL is mentioned below, I want to get JID value.

http://.........../abc.aspx?JID=00001833

I can get complete URL from this code, but I want to get specific value.

string url = driver.Url;
Console.WriteLine(url);
JeffC
  • 22,180
  • 5
  • 32
  • 55
geeta
  • 75
  • 1
  • 1
  • 8
  • possible duplicate of [Getting URL parameter in java and extract a specific text from that URL](http://stackoverflow.com/questions/11733500/getting-url-parameter-in-java-and-extract-a-specific-text-from-that-url) – JeffC Sep 24 '15 at 00:44
  • I totally spaced and entered a Java dup... here's a C# one: http://stackoverflow.com/questions/659887/get-url-parameters-from-a-string-in-net – JeffC Sep 24 '15 at 00:46

3 Answers3

4

UPDATE: As JeffC suggested the proper way to get parameters you should use HttpUtility.ParseQueryString

String yoururl = "http://example.com/abc.aspx?JID=00001833";
Uri theUri = new Uri(yoururl);
String jid = HttpUtility.ParseQueryString(theUri.Query).Get("JID");
Console.WriteLine(jid);

Read more about ParseQueryString here: https://msdn.microsoft.com/en-us/library/ms150046.aspx


*Not recommended way (with string manipulation):

If your jid's length is fix you can do the following:*

string url = driver.Url;
string jid = url.Substring(url.Length-8,8)
Console.WriteLine(jid);
Tamas Toth
  • 359
  • 6
  • 19
  • This is really not the proper way to do this. What if your JID is not 8 digits sometimes? What if there sometimes is another query parameter added to the string after the JID? See the dup I posted for the proper way to do this using `ParseQueryString()` – JeffC Sep 24 '15 at 00:48
0

Your example redone

string url = driver.Url;
string newUrl = url.Split('=').Last();
Console.WriteLine(newUrl);
-1

Try this using string.Split

DotNetFiddle Example You can run

Here is the code from within the fiddle:

var URL = "http://.........../abc.aspx?JID=00001833";
        var JID = URL.Split('?').Last();
        Console.WriteLine(JID);     
        var JIDVal = JID.Split('=').Last();
        Console.WriteLine(JIDVal);  
David Watts
  • 2,249
  • 22
  • 33