htmlexcelvbaselenium-webdriver

Selenium not getting the information from VBA "NoSuchElementError"


This is my first StackOverflow post ever. I'm trying to copy data from a table in a web, the problem is that is my first time using Selenium. I was trying to use FindElementByClass but it says that it doesnt exist.

My code is this:

URLtexto = "https://www.scopus.com/sources.uri" '"https://www.bing.com/search?q="
Dim dd As Variant

Dim bot As New WebDriver
    bot.Start "edge", "https://www.google.com"
    bot.Get URLtexto
    bot.Wait 5000
    
    dd = bot.FindElementByClass("table").FindElementByClass("lightGreyBorderBottom")(1).Text

The snipped HTML from the web that I'm trying to copy is this:

<table class="table main-table" id="sourceResults" role="grid" aria-readonly="true">
  <tbody>
    <tr role="row" id="1" class="lightGreyBorderBottom">
      <td style="width: 6%;">
        <div id="28773div" class="checkbox">
          <input id="source28773" name="selectedSources" type="checkbox" value="28773" onclick="sourceSelection.toggleSourceCheckbox(this.form, this)">
          <label for="source28773" class="checkbox-label">1</label>
        </div>
      </td>
      <td style="width: 35%;">
        <a href="/sourceid/28773" title="View details for this source.">Ca-A Cancer Journal for Clinicians</a>
        <div class="sourceListOutwardLinks marginTopHalf"></div>
      </td>
      <td style="width: 11%;">873.2</td>
      <td style="width: 15%;">
        <a href="/sourceid/28773#tabs=1" title="View CiteScore rank and trend for this source.">99%</a>
        <div class="additionalContent"></div>
        <a href="/sourceid/28773#tabs=1" title="View CiteScore rank and trend for this source."><span>1/404</span></a>
        <br>
        <span>Oncology</span>
      </td>
      <td style="width: 11%;">92,555</td>
      <td style="width: 11%;">106</td>
      <td style="width: 11%;">95</td>
      <td class="noWidth" style="width: 11%;">167.948</td>
      <td class="noWidth" style="width: 11%;">106.094</td>
      <td class="noWidth" style="width: 11%;">John Wiley &amp; Sons</td>
    </tr>
    <tr role="row" id="2" class="lightGreyBorderBottom">
      <td style="width: 6%;">
        <div id="20315div" class="checkbox">
          <input id="source20315" name="selectedSources" type="checkbox" value="20315" onclick="sourceSelection.toggleSourceCheckbox(this.form, this)">
          <label for="source20315" class="checkbox-label">2</label>
        </div>
      </td>
      <td style="width: 35%;">
        <a href="/sourceid/20315" title="View details for this source.">Nature Reviews Molecular Cell Biology</a>
        <div class="sourceListOutwardLinks marginTopHalf"></div>
      </td>
      <td style="width: 11%;">173.6</td>
      <td style="width: 15%;">
        <a href="/sourceid/20315#tabs=1" title="View CiteScore rank and trend for this source.">99%</a>
        <div class="additionalContent"></div>
        <a href="/sourceid/20315#tabs=1" title="View CiteScore rank and trend for this source.">
          <span>1/410</span>
        </a>
        <br>
        <span>Molecular Biology</span>
      </td>
      <td style="width: 11%;">34,204</td>
      <td style="width: 11%;">197</td>
      <td style="width: 11%;">92</td>
      <td class="noWidth" style="width: 11%;">19.301</td>
      <td class="noWidth" style="width: 11%;">35.91</td>
      <td class="noWidth" style="width: 11%;">Springer Nature</td>
    </tr>
  </tbody>
</table>

I want to copy the full table, in parts, with all the data, but VBA contiues giving me the error "NoSuchElement", "Element not found for Class=lightGreyBorderBottom"

I would appreciate your help.

Best regards.

I've tried differents *.FindElementByXYZ" and it didn't work. I'm not good with Selenium.

I've tried to use FindElementByTag("tr")(0).text and nothing


Solution

  • Try another approach. First identify the table, then get all rows in the table, then get each row text:

    Dim trs As WebElements, tbl As WebElement
    Set tbl = bot.FindElementByClass("main-table")
    Set trs = tbl.FindElementsByTag("tr")
    
    For idx = 1 To trs.Count
        dd = trs.Item(idx).Text
        'place dd...
    Next idx