Extract text using css selector

2019-08-22 17:40发布

I am trying to extract specific text using a CSS selector. Here's a screenshot of the part that I would like to extract

enter image description here

I tried

div[id="Section3"]:first-child

but this doesn't return anything. I can't depend on locating the element by the text because I need to extract that text as shown.

This is the relevant HTML

<div class="ad24123fa4-c17c-4dc5-9aa5-ea007a8db30e-5" style="top:8px;left:218px;width:124px;height:31px;text-align:center;">
    <table width="113px" border="0" cellpadding="0" cellspacing="0">
        <tbody>
            <tr>
                <td>
                    <table width="100%" border="0" cellpadding="0" cellspacing="0">
                        <tbody>
                            <tr>
                                <td align="center">
                                    <span class="fcb900b29f-64d7-453d-babf-192e86f17d6f-7">نظامي</span>
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </td>
            </tr>
        </tbody>
    </table>
</div>

The full HTML is here.

This is my try

            On Error Resume Next
            Set ele = .FindElementByXPath("//span[text()='ãäÇÒá']")
            If ele Is Nothing Then sStatus = "äÙÇãí" Else sStatus = "ãäÇÒá"
        On Error GoTo 0

While inspecting the element I noticed that there is a hint of using $0 in the console .. Can this be useful? enter image description here

As for the two possible texts "نظامي" and "منازل"

1条回答
Evening l夕情丶
2楼-- · 2019-08-22 18:36

To use xpath with multiple possible search values use the following syntax:

//*[text()='نظامي' or text()='منازل']

CSS selectors (that work for me):

driver.findElementByCss("#ctl00_ContentPlaceHolder1_CrystalReportViewer1 div.ad071889d2-8e6f-4755-ad7d-c44ae0ea9fca-5 table span").text

which is an abbreviation of the full selector:

#ctl00_ContentPlaceHolder1_CrystalReportViewer1 > tbody > tr > td > div > div.crystalstyle > div.ad071889d2-8e6f-4755-ad7d-c44ae0ea9fca-5 > table > tbody > tr > td > table > tbody > tr > td > span

You can also index into table nodeList

Set matches = html.querySelectorAll("#ctl00_ContentPlaceHolder1_CrystalReportViewer1 div.crystalstyle table")
ActiveSheet.Cells(1, 1) = matches.item(80).innerText

Otherwise:

Reading in from html file I can take the last index of the matches based on class selector. For selenium you would switch to:

driver.FindElementsByCss(".fc180999a8-04b5-46bc-bf86-f601317d19c8-7").count

VBA:

Option Explicit
Public Sub test()
    Dim html As HTMLDocument, matches As Object
    Dim fStream  As ADODB.Stream
    Set html = New HTMLDocument
    Set fStream = New ADODB.Stream
    With fStream
        .Charset = "UTF-8"
        .Open
        .LoadFromFile "C:\Users\User\Desktop\Output6.html"
        html.body.innerHTML = .ReadText
        .Close
    End With

    Set matches = html.querySelectorAll(".fc180999a8-04b5-46bc-bf86-f601317d19c8-7")

    ActiveSheet.Cells(1, 1) = matches.item(matches.Length - 1).innerText
End Sub
查看更多
登录 后发表回答