【问题标题】:Problem getting text from website using selenium vba使用 selenium v​​ba 从网站获取文本的问题
【发布时间】:2021-01-18 22:22:51
【问题描述】:

我正在尝试将一些数据从网站获取到 Excel 中......如果我在 Chrome 中使用 View pagesource 它会显示

  <div class="buttons">
    <div class="vote" onclick="Vote.cast();" data-tooltip-text="Stem op deze foto">
      <div class="container">
        <div class="voteIcon">Stem op deze foto!</div>
        <div class="votes"></div>
      </div>
    </div>
  </div>

当我检查它时,它会显示

<div class="buttons">
    <div class="vote voted" onclick="Vote.cast();" data-tooltip-text="Stem op deze foto">
      <div class="container">
        <div class="voteIcon">Stem op deze foto!</div>
        <div class="votes">2</div>
      </div>
    </div>
  </div>
  

数字 2(在本例中)是我要提取的文本....但无论我尝试什么,它都只返回一个空字符串。

ActiveWorkbook.ActiveSheet.Cells(i, 2).Value = detailbot.FindElementByClass("votes").text

在我看来应该可以解决问题,但事实并非如此

【问题讨论】:

标签: html excel vba selenium web-scraping


【解决方案1】:

我从未使用过 vba,但我发现了这个问题:https://github.com/7Gabriel/selenium-vba/issues/126#issuecomment-210692163

似乎detailbot.findElementByClassName("votes").Text 应该返回一个值。

【讨论】:

    【解决方案2】:

    它存储在innerHTML/textContent 属性中。试试:

    ActiveWorkbook.ActiveSheet.Cells(i, 2).Value = detailbot.FindElementByClass("votes").Attribute("innerHTML")
    

    ActiveWorkbook.ActiveSheet.Cells(i, 2).Value = detailbot.FindElementByClass("votes").Attribute("textContent")
    

    node.textContent

    Node接口的textContent属性表示文本 节点及其后代的内容。

    textContent 获取所有元素的内容,包括&lt;script&gt;&lt;style&gt; 元素。相比之下,innerText 只显示“人类可读” 元素。

    textContent 返回节点中的每个元素。相比之下,innerText 知道样式并且不会返回“隐藏”元素的文本。

    【讨论】:

      【解决方案3】:
      Set MyBy = New By
      While detailbot.IsElementPresent(MyBy.XPath("//div[text()='" & j & "']")) = False
          j = j + 1
      Wend
      ActiveWorkbook.ActiveSheet.Cells(i, 2).Value = j
      

      【讨论】:

        【解决方案4】:

        获取 Chrome 扩展 RUTO XPath Finder。使用 Ruto,您可以单击一个元素,并获取其 Xpath。

        debug.print CD.FindElementByXPath("").Text
        debug.print CD.FindElementByXPath("").value
        

        您可以使用它,以便在运行 ChromeDriver 时显示 Ruto 扩展。

        Private Const My_Extension As String = "C:\Users\username\AppData\Local\Google\Chrome\User Data\Default\Extensions\ilcoelkkcokgeeijnopjnolmmighnppp\7.0.0_0" 'RUTO XPATH
        cd.AddArgument "load-extension=" & My_Extension 
        

        您可以使用它来循环浏览页面上的所有元素。我添加了 .ScrollIntoView,因为在某些页面上,元素在它们出现之前是不可见的。我添加了 DoEvents 以暂停一秒钟以防页面需要加载,如果页面加载有问题,您可以尝试等待更长时间,或者直到元素出现在页面上。如果.Text没有返回值,试试.value

        Dim ElemCounter As Long
        Dim MyBy As By
        Set MyBy = New By
        
        ElemCounter = 2
        Do Until CD.IsElementPresent(MyBy.XPath("//div[text()='" & ElemCounter & "']")) = False
            CD.FindElementByXPath("//div[text()='" & ElemCounter & "']").ScrollIntoView
            Debug.Print CD.FindElementByXPath("//div[text()='" & ElemCounter & "']").Text
            Debug.Print CD.FindElementByXPath("//div[text()='" & ElemCounter & "']").Value
            ElemCounter = ElemCounter + 1
            DoEvents
        Loop
        

        【讨论】:

        • RUTO 返回这个 Xpath: //div[text()='2'] 但“2”会改变....这就是我要读取的值
        • 感谢 Manon 的澄清。我编辑了答案以循环浏览页面上的元素。如果第一个元素总是以 2 开头,则设置 ElemCounter = 2,否则更改 ElemCounter = 1 或页面上的第一个元素编号。
        猜你喜欢
        • 2019-04-02
        • 2019-01-29
        • 1970-01-01
        • 2020-10-31
        • 1970-01-01
        • 1970-01-01
        • 2018-01-15
        • 2018-11-14
        相关资源
        最近更新 更多