这是我第一篇 StackOverflow 帖子。 我正在尝试从网络中的表复制数据,问题是这是我第一次使用 Selenium。我试图使用 FindElementByClass 但它说它不存在。
我的代码是这样的:
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
我试图复制的从网络上截取的 HTML 是这样的:
<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 & 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>
我想部分复制完整的表格以及所有数据,但 VBA 继续给我错误“NoSuchElement”,“找不到 Class=lightGreyBorderBottom 的元素”
非常感谢您的帮助。
致以诚挚的问候。
我尝试过不同的 *.FindElementByXYZ" 但它不起作用。我不擅长使用 Selenium。
我尝试使用 FindElementByTag("tr")(0).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