-2
 string str = "&action";
 char[] characters = str.ToCharArray();
 IWebDriver driver = new ChromeDriver("C:/Users/kathiravan/Desktop/New Algo/");
 driver.Navigate().GoToUrl("https://kite.zerodha.com/connect/login?v=3&api_key=sjc0vn34q8tu05czx7");
 RequestToken = driver.Url.Split('=')[1].Split(characters)[0];

From the above URL I am navigating to another URL and split that URL, used to get two different types:

  1. https://zerodha.com/?status=success&request_token=IFmqdfS3QYw3SZqoeiuCPLCsT2BaWN2I&action=login from this url i want only value IFmqdfS3QYw3SZqoeiuCPLCsT2BaWN2I some times the same url
  2. https://zerodha.com/?status=success&request_token=IFmqdfS3QYw3SZqoeiuCPLCsT2BaWN2I the required value will be at the end IFmqdfS3QYw3SZqoeiuCPLCsT2BaWN2I

I want to get value after 'request_token=' and before '&action' sometime at the end how to achieve, I have tried all the possibilities but negative. Any one can help this issue?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
  • Does this answer your question? [How do we get the specific value from URL using Selenium Webdriver C#?](https://stackoverflow.com/questions/32737310/how-do-we-get-the-specific-value-from-url-using-selenium-webdriver-c) – Pranav Singh Jun 22 '21 at 09:35

1 Answers1

2

This regex should solve your problem:

request_token=\K([a-zA-Z0-9]+)

Explanation:

"request_token=": Find this text literally, so you get only the token and not some other path variable.

"\K": Resets the starting point of the match, basically excluding "request_token=" from the final match.

"[a-zA-Z0-9]+": This is the token, any alphanumeric character and should be at least size of 1 (because of "+"), this part is wrapped in parentheses so you could extract a group from the match, but is not a must since you need the entire match with this regex.

Code:

Match match = Regex.Match(driver.Url, "request_token=\K([a-zA-Z0-9]+)")
if (Match.Success)
    string token = match.Groups[1]

Alternatively if you do not want to use the "\K" character, you could always get the match with the "request_token=" string:

(request_token=)([a-zA-Z0-9]+)

In this case the 1st group will be "request_token=" literally, get the 2nd group would be the token.

Kfir Doron
  • 229
  • 2
  • 5