Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.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
Excel 使用css选择器提取文本_Excel_Vba_Selenium Webdriver_Xpath_Web Scraping - Fatal编程技术网

Excel 使用css选择器提取文本

Excel 使用css选择器提取文本,excel,vba,selenium-webdriver,xpath,web-scraping,Excel,Vba,Selenium Webdriver,Xpath,Web Scraping,我正在尝试使用CSS选择器提取特定文本。这是我想提取的部分的屏幕截图 我试过了 div[id="Section3"]:first-child 但这不会返回任何内容。我不能依靠文本来定位元素,因为我需要提取文本,如图所示 这是相关的HTML <div class="ad24123fa4-c17c-4dc5-9aa5-ea007a8db30e-5" style="top:8px;left:218px;width:124px;height:31px;text-align:center;"&g

我正在尝试使用CSS选择器提取特定文本。这是我想提取的部分的屏幕截图

我试过了

div[id="Section3"]:first-child
但这不会返回任何内容。我不能依靠文本来定位元素,因为我需要提取文本,如图所示

这是相关的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>
在检查元素时,我注意到控制台中有使用$0的提示。。这有用吗?


至于两个可能的文本“نظامي”和“مازل”

要使用xpath和多个可能的搜索值,请使用以下语法:

//*[text()='نظامي' or text()='منازل']
CSS选择器(适用于我):

这是完整选择器的缩写:

#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
您还可以索引到表nodeList中

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

从html文件中读取,我可以根据类选择器获取匹配的最后一个索引。对于selenium,您可以切换到:

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


在那一点上HTML是什么样子的?我已经更新了帖子,这是一些非常不友好的自动化HTML。我假设您已经删除了很多相关的HTML,因为这两个表都是空的。在所需文本周围是否有任何文本标签可以用作锚定?我看不懂阿拉伯语,但有点像<代码>名字:John您想在哪里看到文本“John”?我正在使用selenium来完成这部分。非常感谢您的回复。至于.计数,我得到0。。“fc180999a8-04b5-46bc-bf86-f601317d19c8-7”类不是固定的,它每次都会更改。。xpath会更容易吗?有两种可能的文本。是否可以同时包含这两个元素并检查这两个元素的现有元素?我已经发布了我的try,但是没有正常工作,我得到的字符串变量的结果不正确这是完美的
/*[text()='。非常感谢这个解决方案,我想放一些其他锚来指代那个部分或周围。。。你看到0美元的小费了吗?那可能有用吗?非常感谢我的导师。我非常感谢所有这些了不起的帮助
Set matches = html.querySelectorAll("#ctl00_ContentPlaceHolder1_CrystalReportViewer1 div.crystalstyle table")
ActiveSheet.Cells(1, 1) = matches.item(80).innerText
driver.FindElementsByCss(".fc180999a8-04b5-46bc-bf86-f601317d19c8-7").count
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