Home > database >  How to extract text from TextNode using Selenium in VBA
How to extract text from TextNode using Selenium in VBA

Time:08-21

I'm trying to do a macro and I can't find a solution for a problem. I'm using Selenium to get data from a website. Here is the HTML:

<ul >
<li>
<img src="=" title="Red" data-pagespeed-url-hash="820105347" onl oad="pagespeed.CriticalImages.checkImageForCriticality(this);">
<span >time 20/08/2022 12:46</span>
<strong>status Objeto aguardando retirada no endereço indicado</strong>
<br>
where it's Agência dos Correios - CONTAGEM/MG
<br>
<small>6&nbsp;horas, 2&nbsp;minutos atrás</small>
</li>

I want to get the time, status and where it is.

The first two I was able to get using:

Cells(linha, 12).value = navegadorChrome.FindElementsByClass("milestones")(1).FindElementsByTag("span")(1).Text
Cells(linha, 13).value = navegadorChrome.FindElementsByClass("milestones")(1).FindElementsByTag("strong")(1).Text

The last I can't get, it's the part after the first <br>.

Any help will be appreciated

CodePudding user response:

To extract the desired texts you can use either of the following locator strategies:

  • Extracting time:

    • Using Css:

      Cells(linha, 12).value = navegadorChrome.FindElementByCss("ul.milestones > li span.out").Text
      
    • Using XPath:

      Cells(linha, 12).value = navegadorChrome.FindElementByXPath("//ul[@class='milestones']/li//span[@class='out']").Text 
      
  • Extracting status:

    • Using Css:

      Cells(linha, 12).value = navegadorChrome.FindElementByCss("ul.milestones > li strong").Text
      
    • Using XPath:

      Cells(linha, 12).value = navegadorChrome.FindElementByXPath("//ul[@class='milestones']/li//strong").Text 
      
  • Extracting where it's:

    • Using Css:

      Cells(linha, 12).value = navegadorChrome.ExecuteScript('return arguments[0].childNodes[5].textContent;', navegadorChrome.FindElementByCss('ul.milestones > li'))
      
    • Using XPath:

      Cells(linha, 12).value = navegadorChrome.ExecuteScript('return arguments[0].childNodes[5].textContent;', navegadorChrome.FindElementByXPath("//ul[@class='milestones']/li")) 
      
  • Related