我们如何使用 Selenium Webdriver C# 从 URL 中获取特定值?

我们如何使用 Selenium Webdriver C# 从 URL 中获取特定值?

问题描述:

下面提到了我的页面 URL,我想获取 JID 值.

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

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

我可以从这段代码中获取完整的 URL,但我想获取特定的值.

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

string url = driver.Url;
Console.WriteLine(url);

更新:由于 JeffC 建议了获取参数的正确方法,您应该使用 HttpUtility.ParseQueryString

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);

在此处阅读有关 ParseQueryString 的更多信息:https:///msdn.microsoft.com/en-us/library/ms150046.aspx

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

*不推荐的方式(使用字符串操作):

*Not recommended way (with string manipulation):

如果您的 jid 的长度是固定的,您可以执行以下操作:*

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);