c# 在 webbrowser 中读取不断变化的 Progressbar 的值

c# read the value of a changing Progressbar in webbrowser

我是 c# 的新手(之前在 delphi 学习过),我很难找到这个:

网站的Html代码是这样的:

<div class="progress-bar progress-bar-danger" id="counter" style="width: 10.%; overflow: hidden;"></div>

我正在想办法解决这个问题:

var CheckValue = webBrowser1.Document.GetElementById("counter"); if (counter.style.width > 70%) { //code }

基本上我想做的是: 我想检查网站上进度条的宽度是否填充超过 70%,如果是,它将执行代码,但如果不是,它将在几秒钟后重试。

如果您需要更多信息,请告诉我!

谢谢

您可以使用 CheckValue.Style, which will return a string containing the style. Then you can use Regex 找到您要查找的内容。

您希望您的正则表达式匹配 width:.% 之间的数字。您可以为此使用它:

width: ([0-9]+(\.[0-9]+)?)\.?%

这将匹配每个以 width: 开头并以 % 结尾的字符串,可能在 % 之前有一个 .,0 之间至少有 1 个字符和 9.

您可以使用此代码获取此值:

var checkValue = webBrowser1.Document.GetElementById("counter");

Regex regex = new Regex("width: ([0-9]+(\.[0-9]+)?)\.?%");
Match match = regex.Match(checkValue.Style);

// Check if match found
if (match.Groups.Count > 1)
{
    String s = match.Groups[1].ToString();
    int width = (int)Convert.ToDouble(s);
}