1

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 class="milestones">
<li>
<img src="=" title="Red" data-pagespeed-url-hash="820105347" onload="pagespeed.CriticalImages.checkImageForCriticality(this);">
<span class="out">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

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

1 Answers1

0

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")) 
      
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • I guess my question wasnt clear enough or i didnt understand your answer: the first 2 items are working (status and time), only the 3rd "where it's" i cant get it's the part after the BR – Fernando Barreto Aug 20 '22 at 22:55
  • i'm sorry again, but i didnt get what you mean with flaky (sorry, english isn't my native language) Anyway, I used your last suggestion and worked, i just needed to change childNodes[5] -> childNodes[8] the only downside is that it's copying the
    on it and making a break line, i added a small code to remove after the data collection. Thanks
    – Fernando Barreto Aug 20 '22 at 23:21