在VBA中使用Selenium提取文本

jfewjypa  于 2023-03-18  发布在  其他
关注(0)|答案(1)|浏览(139)

我想从这个网站获取一些信息:https://www.websro.com.br/rastreamento-correios.php?P_COD_UNI=LX991645039CN

我需要状态(在此页面上)- Objeto recebido pelos Correios do Brasil
日期-日期:2023年3月17日07:57
位置-本地:库里蒂巴/公共关系

我尝试了很多方法,但都不管用:

Cells(linha, 12).value = navegadorChrome.FindElementsByClass("container")(1).FindElementsByCss("row")(4).Text
    
Cells(linha, 13).value = navegadorChrome.FindElementsByClass("container")(1).FindElementsByclassName("bold eventoLocal")(1).Text

差点就成功了,使用:

Cells(linha, 13).value = navegadorChrome.FindElementsByClass("container")(1).FindElementByXPath("/html/body/div[1]/div[4]/div[1]/div[1]/table[1]/tbody[1]/tr[1]/td[1]/span[1]/").Text

但它获取span之间的文本,我需要/span之后的文本
没有一个工作,试图在互联网上找到什么都没有。谢谢。

lztngnrs

lztngnrs1#

我能做的最多就是-

Option Explicit

Sub sbCorreios()
' https://stackoverflow.com/questions/75772068/extracting-text-using-selenium-in-vba
' https://www.websro.com.br/rastreamento-correios.php?P_COD_UNI=LX991645039CN
    Dim driver As ChromeDriver
    Set driver = New ChromeDriver
    Dim we As WebElement
    Dim sURL, sSourceCode As String
    sURL = "https://www.websro.com.br/rastreamento-correios.php?P_COD_UNI=LX991645039CN"
    Dim sFilename As String
    Call driver.Start("edge", sURL)
    driver.get (sURL)
    driver.Window.Maximize
    sbDelay (100000)
    Set we = driver.FindElementByXPath("/html/body")
    MsgBox we.FindElementByTag("tbody").Text
    driver.Quit
End Sub
Sub sbDelay(delay As Long): Dim i As Long: For i = 1 To delay:  DoEvents: Next i: End Sub 'old skool delay

但不是Msgbox,而是将tbody元素分配给一个字符串,然后使用MID()函数剪切该字符串
https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/mid-function

相关问题