Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#获取html元素的属性_C# - Fatal编程技术网

C#获取html元素的属性

C#获取html元素的属性,c#,C#,我有这段代码 <button value="1" class="_42ft _4jy0 _n6m _4jy3 _517h _51sy" data-hover="tooltip" aria-label="Start a video call with Tsiato" type="submit" id="js_rk"><i class="_e0b img sp_qk8sNUxukfD sx_4816f8"></i></button> 我可以找到该类,

我有这段代码

<button value="1" class="_42ft _4jy0 _n6m _4jy3 _517h _51sy" data-hover="tooltip" aria-label="Start a video call with Tsiato" type="submit" id="js_rk"><i class="_e0b img sp_qk8sNUxukfD sx_4816f8"></i></button>
我可以找到该类,但无法获取属性“aria label”,以查看它是否包含任何“Start”文本。。。
你能告诉我这里怎么了吗\

因此,如果有一个按钮不包含aria label作为属性,那么您将获得nullreference异常,因为您正在对null使用Contains()方法

所以试试这个:

        String ariaLabel = curElement.GetAttribute("aria-label");
        if (ariaLabel != null && ariaLabel.Length != 0)
        {
            if(ariaLabel.Contains("Start a video call"))
            {
               // do your stuff
            }
        }
你可以试试这个-

var p = "<button value='1' class='_42ft _4jy0 _n6m _4jy3 _517h _51sy' data-hover='tooltip' aria-label='Start a video call with Tsiato' type='submit' id='js_rk'><i class='_e0b img sp_qk8sNUxukfD sx_4816f8'></i></button>";
var k = new XmlDocument();
k.Load(new MemoryStream(Encoding.UTF8.GetBytes(p.ToCharArray())));
Console.WriteLine(k.GetElementsByTagName("button")[0].Attributes["aria-label"].Value);
var p=”“;
var k=新的XmlDocument();
k、 加载(新内存流(Encoding.UTF8.GetBytes(p.tocharray()));
Console.WriteLine(k.GetElementsByTagName(“按钮”)[0]。属性[“aria标签”].Value);
  • 您的class=属性的名称为“class”,而不是“classname”
  • 如果您的方法GetAttribute(string)可以返回null,则应始终检查返回的值

    if (curElement.GetAttribute("classname") == "_42ft _4jy0 _n6m _4jy3 _517h _51sy" 
    && (curElement.GetAttribute("aria-label") ?? "").Contains("Start a video call"))
    {
        label5.Text = "online";
    }
    

  • 您会遇到什么错误?NullReferenceException如果它在某处使用DOM对象,可能需要使用“ariaLabel”?猜猜看,你解决这个问题了吗?
    if (curElement.GetAttribute("classname") == "_42ft _4jy0 _n6m _4jy3 _517h _51sy" 
    && (curElement.GetAttribute("aria-label") ?? "").Contains("Start a video call"))
    {
        label5.Text = "online";
    }