如何在标签内获取 img alt 的文本<a>



我有一个包含以下html部分的url

<div class="shop cf">
<a class="shop-logo js-shop-logo" href="/m/3870/GMobile">
<noscript>
<img alt="GMobile" class="js-lazy" data-src="//a.scdn.gr/ds/shops/logos/3870/mid_20160920155600_71ff515d.jpeg" src="//a.scdn.gr/ds/shops/logos/3870/mid_20160920155600_71ff515d.jpeg" /> 
</noscript>
<img alt="GMobile" class="js-lazy" data-src="//a.scdn.gr/ds/shops/logos/3870/mid_20160920155600_71ff515d.jpeg" src="//c.scdn.gr/assets/transparent-325472601571f31e1bf00674c368d335.gif" />
</a>
</div>

我想在div类商店中获得第一个img alt,我做

Set seller = Doc.querySelectorAll("img")      
wks.Cells(i, "D").Value = seller.getAttribute("alt").Content(0)

我什么都没得到,我忘了包括什么?!?

我能从拿到吗

<noscript>

标签?

我也尝试了以下

Set seller = Doc.getElementsByClassName("js-lazy")
wks.Cells(i, "D").Value = seller.getAttribute("alt")

使用带有属性选择器的元素

CSS:

img[alt]

VBA:

ie.document.querySelector("img[alt]")

您可能需要添加

ie.document.querySelector("img[alt]").getAttribute("alt")

要包含该类,请使用

ie.document.querySelector("img.js-lazy[alt]")

如果有多个元素,则使用querySelectorAll并索引到返回的nodeList中,例如

Set list = ie.document.querySelectorAll("img.js-lazy[alt]")
list.item(0).getAttribute('alt')
list.item(1).getAttribute('alt')

你尝试过这种方法吗?

let lazy1 = document.querySelectorAll(".js-lazy")[0]
let lazyalt = lazy1.getAttribute("alt");
let shop = document.querySelector('.shop');
shop.classList.add(lazyalt);
console.log(lazyalt)

最新更新