我们如何使用 Selenium Webdriver C# 从 URL 中获取特定值? [英] How do we get the specific value from URL using Selenium Webdriver C#?

查看:34
本文介绍了我们如何使用 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);

这篇关于我们如何使用 Selenium Webdriver C# 从 URL 中获取特定值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆