简体   繁体   English

从html网页中的label元素中提取“ for”属性

[英]Extracting the “for” attribute from a label element in an html webpage

I have some code that analyses various attributes of a webpage when parts of that webpage are clicked. 当单击部分网页时,我有一些代码可以分析该网页的各种属性。 One of the elements which is picked out is the ID of the clicked element. 选中的元素之一是单击元素的ID。

Sometimes however there is no ID and instead the element being clicked on is a label using the "for" attribute to reference an ID. 但是,有时没有ID,而是单击的元素是使用“ for”属性引用ID的标签。 In these case I want to pick up the "for" attribute value. 在这种情况下,我想选择“ for”属性值。

I have attempted to do this as follows: 我尝试这样做,如下所示:

txtID.Text = TryCast(myHTMLDocument, HtmlDocument).GetElementFromPoint(lastMousePos).GetAttribute("id")
If txtID.Text = "" Then
  txtID.Text = TryCast(myHTMLDocument, HtmlDocument).GetElementFromPoint(lastMousePos).GetAttribute("for")
End If

For some reason .GetAttribute("for") always returns blank. 由于某些原因, .GetAttribute("for")始终返回空白。 Am I referencing this attribute wrongly - or is something else going on. 我是错误地引用了此属性-还是其他情况。

HTML Example below: 下面的HTML示例:

<div class="question legal-owner active">

<a class="help-trigger help-trigger-layout">
    <span class="help-text-icon"></span>
</a>


<div class="quote-help quote-help-layout">
    <a class="quote-help-close-container">
        <div class="quote-help-close"></div>
    </a>
    <h3>Car ownership</h3>

    <p>
        We need to know whether the car belongs to you. If you don’t own the car but you’re the registered keeper, you should answer ‘No’ 
        (the owner of the car and the registered keeper can be different people).
    </p>

</div>

<span class="editor-label question-layout">
    <label for="OwningAndUsingCarPanel_LegalOwner">Are you (or will you be) the legal owner of this car?</label>
</span> 
    <ul class="question-layout yesno-radio-list">
        <li>
            <input name="OwningAndUsingCarPanel.LegalOwner" id="OwningAndUsingCarPanel_LegalOwner_true" type="radio" value="True">
            <label for="OwningAndUsingCarPanel_LegalOwner_true">
                <span>Yes</span>
            </label>
        </li>
        <li>
            <input name="OwningAndUsingCarPanel.LegalOwner" id="OwningAndUsingCarPanel_LegalOwner_false" type="radio" value="False">
            <label for="OwningAndUsingCarPanel_LegalOwner_false">
                <span>No</span>
            </label>
        </li>
    </ul>
<span class="editor-validation">
    <span class="field-validation-valid" id="OwningAndUsingCarPanel_LegalOwner_validationMessage"></span>
</span>
</div>

I have resolved this by creating my own function called getUnknown to search for an attribute within a tag. 我已经通过创建自己的名为getUnknown的函数来搜索标记中的属性来解决此问题。 This should work for any attribute whose value is surrounded by double quotes. 此方法适用于其值用双引号引起来的任何属性。 The function has 2 arguments, the first is a string which should contain the element tag complete with attributes and values, and the second is the attribute that you want to extract the value for. 该函数有2个参数,第一个是一个字符串,其中应包含带有属性和值的element标签,第二个是要为其提取值的属性。

Private Function getUnknown(myText As String, myAttr As String)
    Dim myResult As String = ""
    Dim myStart As Integer = 0
    Dim myLen As Integer = 0
    'remove any spaces around the "=" sign
    Dim myCleanText As String = Regex.Replace(myText, "\s+([=])\s+|\s+([=])|([=])\s+", "=")
    'add =" to the attribute to avoid finding non-attributes when using IndexOf function
    Dim myFullAttr As String = myAttr.Trim().ToLower + "="""

    Try
        myStart = myCleanText.ToLower().IndexOf(myFullAttr)
        If myStart = -1 Then
            myResult = "Nothing Found"
        Else
            myStart = myStart + myFullAttr.Length
            myLen = myCleanText.IndexOf("""", myStart) - myStart
            myResult = myCleanText.Substring(myStart, myLen)
        End If
    Catch ex As Exception
        myResult = "Nothing Found"
    End Try

    Return myResult

End Function

In the context of my original question I have used this as follows 在我最初的问题中,我使用了以下方法

Dim myElement As String = _
 TryCast(myHTMLDocument, HtmlDocument).GetElementFromPoint(lastMousePos).OuterHtml.ToString

txtID.Text = getUnknown(myElement, "for")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM