提问人:Artist 提问时间:2/12/2023 更新时间:2/12/2023 访问量:43
无法获得硒元素
Can't get Selenium element
问:
<div class="sc-jTtXpN btfdrL"><div class="sc-jvvemr gyzva"><div data-test-component="ProductOrAdCard" class="sc-byxFCI ytiGe"><div><span class="sc-llGDqb sc-gqgnwQ fEAASo hZGRky"><a href="/krasnodar/dom-dacha/shkafy-komody/shkaf-kupie-603a3c247ff6205de83754f7" title="Шкаф купе" rel="noopener noreferrer" style="cursor: pointer;"><figure data-test-component="ProductCard" data-test-id="603a3c247ff6205de83754f7" class="sc-hsZwpi fssEAr"><div class="sc-gcHwEF sc-fkxeQW eHsIOL jskOCU"><div class="sc-mxuny kRlvNr"><svg class="sc-dOrDzm uyVta sc-hJMuen ipuCdo" role="img" aria-label=""><image preserveAspectRatio="xMidYMid slice" xlink:href="https://cdn0.youla.io/files/images/360_360/60/3a/603a3c0ef45aac13f07f4fc6-1.jpg" width="100%" height="100%" x="0" y="0"></image></svg></div><div data-test-component="Badges" class="sc-jUOgmM heJjNv"><div class="sc-jcTjWU eYjgWi"><div class="sc-fkMAoN zWPKM"><span class="sc-cOxWqc sc-eipBjq jfMydn gatWGl">Краснодар</span></div></div></div></div><div class="sc-gKelgU joeTcw"><div class="sc-fNYidB hcfixG"><div class="sc-jRZndg kkuRyV"><p data-test-block="ProductPrice" class="sc-cOxWqc sc-gtBfHm iIywHL kSsyhU"><span data-test-component="Price" class="sc-dbkbXZ izEAeu sc-hZjMBx grkrjO"><span class="sc-dXqfbs iMealh">33 100</span> <span class="sc-jGoKbf bKjCVH"><span>₽</span><i class="sc-kCuNey irLxOX">руб.</i></span></span></p></div><div role="button" class="sc-kHvPkg jsngjZ"><svg width="40" height="13" viewBox="0 0 40 13" fill="currentColor" class="sc-hEBzJi dTJGgC"><path d="M0 12v-.011A4.003 4.003 0 0 0 3.487 9.29C4.993 3.778 9.845 0 16 0h8c6.155 0 11.007 3.778 12.513 9.29A4.003 4.003 0 0 0 40 11.99V13H0v-1z"></path></svg><svg width="30" height="9" viewBox="0 0 30 9" fill="currentColor" class="sc-dMuQbb fIjttd"><path d="M27.865 8H0v-.004A3.002 3.002 0 0 0 2.67 6h.007c1.407-3.617 4.88-6 9.165-6h6.316c4.285 0 7.758 2.383 9.165 6h.008A3.002 3.002 0 0 0 30 7.996V8h-2.135zM15.088 24h-.18.18zM0 8h30v1H0V8z"></path></svg><div><div class="sc-jPNehe FpavK"><div data-test-action="FavoriteToggleClick" class="sc-jlqnSw udMMr"><div class="sc-jjeeWu lnUail"><svg width="24" height="24" viewBox="0 0 24 24"><path fill="#f75059" fill-rule="evenodd" d="M16 3c3.59 0 6 2.41 6 6 0 4.452-3.44 8.308-9.311 11.824-.394.246-.98.246-1.366.006C5.46 17.353 2 13.466 2 9c0-3.59 2.41-6 6-6 1.39 0 2.746.61 4 1.641C13.254 3.61 14.61 3 16 3z"></path></svg></div><div class="sc-jjeeWu dFZxEf"><svg width="24" height="24" viewBox="0 0 24 24" fill="currentColor" class="sc-dOGYXd ikyMDp"><path d="M12.333 5.673L12 5.97l-.333-.298C10.487 4.618 9.229 4 8 4 4.962 4 3 5.962 3 9c0 4.01 3.244 7.656 8.842 10.975a.4.4 0 0 0 .326-.004C17.772 16.615 21 12.996 21 9c0-3.038-1.962-5-5-5-1.229 0-2.488.618-3.667 1.673zM16 3c3.59 0 6 2.41 6 6 0 4.452-3.44 8.308-9.311 11.824-.394.246-.98.246-1.366.006C5.46 17.353 2 13.466 2 9c0-3.59 2.41-6 6-6 1.39 0 2.746.61 4 1.641C13.254 3.61 14.61 3 16 3z"></path></svg></div></div></div></div></div></div><figcaption class="sc-cOxWqc sc-dASMHb sc-cjpfIh eOPaPs jhYtTr jQiGFk"><span data-test-block="ProductName" color="primary" class="sc-cOxWqc sc-fxvcDT bOrVyP dreCvV">Шкаф купе</span></figcaption></div></figure></a></span></div></div><div data-test-component="ProductOrAdCard" class="sc-byxFCI ytiGe"><div><span class="sc-llGDqb sc-gqgnwQ fEAASo hZGRky"><a href="/krasnodar/krasota-i-zdorove/parfyumeriya/lakostie-bielyie-turtsiia-63d9233e35c2d94a671b77a0" title="Лакосте белые турция" rel="noopener noreferrer" style="cursor: pointer;"><figure data-test-component="ProductCard" data-test-id="63d9233e35c2d94a671b77a0" class="sc-hsZwpi fssEAr"><div class="sc-gcHwEF sc-fkxeQW eHsIOL jskOCU"><div class="sc-mxuny kRlvNr"><svg class="sc-dOrDzm uyVta sc-hJMuen ipuCdo" role="img" aria-label=""><image preserveAspectRatio="xMidYMid slice" xlink:href="https://cdn0.youla.io/files/images/360_360/63/d9/63d9233d4939436ff521395f-1.jpg" width="100%" height="100%" x="0" y="0"></image></svg></div><div data-test-component="Badges" class="sc-jUOgmM heJjNv"><div class="sc-jcTjWU eYjgWi"><div class="sc-fkMAoN giYGOi"><div><div class="sc-jPNehe kDqmIb"><svg width="16" height="16" viewBox="0 0 16 16" data-test-block="PaymentIcon" class="sc-dmjyfX gLwsSM"><g fill="#FFF" fill-rule="evenodd"><path d="M14 2.8v6.179c0 2.21-1.492 4.164-3.682 4.824L8 14.5l-2.317-.697C3.492 13.143 2 11.19 2 8.98V2.8l6-1.3 6 1.3zm-6.318 7.988v-.447h1.08a.55.55 0 0 0 .363-.112.378.378 0 0 0 .134-.307.385.385 0 0 0-.134-.31.543.543 0 0 0-.364-.114H7.706v-.685h.938c.501 0 .924-.085 1.267-.253.343-.168.601-.404.774-.707a2.11 2.11 0 0 0 .259-1.06c0-.415-.09-.776-.269-1.082A1.784 1.784 0 0 0 9.894 5c-.342-.166-.754-.25-1.235-.25H7.394c-.342 0-.595.08-.76.241-.164.16-.246.41-.246.75v2.066h-.361c-.18.003-.32.05-.42.14a.465.465 0 0 0-.152.365c0 .155.05.277.152.364.1.087.24.132.42.136h.361v.685h-.435a.541.541 0 0 0-.366.114.388.388 0 0 0-.132.31c0 .13.044.232.132.307.088.074.21.112.366.112h.46v.447a.605.605 0 0 0 .222.376.657.657 0 0 0 .412.134c.16 0 .297-.045.413-.134a.605.605 0 0 0 .222-.376z"></path><path d="M8.322 7.808c.442 0 .766-.078.971-.233.205-.156.308-.414.308-.776 0-.359-.098-.62-.293-.781-.195-.162-.487-.243-.874-.243H7.7v2.033h.62z"></path></g></svg></div></div></div><div class="sc-fkMAoN khwkAL"><div><div class="sc-gnGSMD cAHcTq"><svg width="16" height="16" viewBox="0 0 24 24" data-test-block="DeliveryIcon" class="sc-dmjyfX gLwsSM"><path fill="#fff" fill-rule="evenodd" d="M3.393 11.5l.643-3h6.214a.75.75 0 1 0 0-1.5H4.357l.389-1.814A1.5 1.5 0 0 1 6.213 4h8.932a1.5 1.5 0 0 1 1.466 1.814L16.143 8h.15a3 3 0 0 1 2.581 1.472l2.336 3.947a2 2 0 0 1 .258 1.302l-.223 1.562A2 2 0 0 1 19.265 18h-.436a3.001 3.001 0 0 1-5.658 0H13v-.015c-.07.01-.14.015-.213.015H8.83a3.001 3.001 0 0 1-5.72-.2 1.5 1.5 0 0 1-.72-1.614L3.07 13h6.18a.75.75 0 1 0 0-1.5H3.393zM6 18a1 1 0 1 0 0-2 1 1 0 0 0 0 2zm11.686-7.448a2 2 0 0 0-1.704-.952H15.8l-.9 3.9h4.6l-1.814-2.948zM16 18a1 1 0 1 0 0-2 1 1 0 0 0 0 2z"></path></svg><span class="sc-cOxWqc sc-eipBjq jfMydn jXbdGA">Краснодар</span></div></div></div></div></div></div><div class="sc-gKelgU joeTcw"><div class="sc-fNYidB hcfixG"><div class="sc-jRZndg kkuRyV"><p data-test-block="ProductPrice" class="sc-cOxWqc sc-gtBfHm iIywHL kSsyhU"><span data-test-component="Price" class="sc-dbkbXZ izEAeu sc-hZjMBx grkrjO"><span class="sc-dXqfbs iMealh">800</span> <span class="sc-jGoKbf bKjCVH"><span>₽</span><i class="sc-kCuNey irLxOX">руб.</i></span></span></p></div><div role="button" class="sc-kHvPkg jsngjZ"><svg width="40" height="13" viewBox="0 0 40 13" fill="currentColor" class="sc-hEBzJi dTJGgC"><path d="M0 12v-.011A4.003 4.003 0 0 0 3.487 9.29C4.993 3.778 9.845 0 16 0h8c6.155 0 11.007 3.778 12.513 9.29A4.003 4.003 0 0 0 40 11.99V13H0v-1z"></path></svg><svg width="30" height="9" viewBox="0 0 30 9" fill="currentColor" class="sc-dMuQbb fIjttd"><path d="M27.865 8H0v-.004A3.002 3.002 0 0 0 2.67 6h.007c1.407-3.617 4.88-6 9.165-6h6.316c4.285 0 7.758 2.383 9.165 6h.008A3.002 3.002 0 0 0 30 7.996V8h-2.135zM15.088 24h-.18.18zM0 8h30v1H0V8z"></path></svg><div><div class="sc-jPNehe FpavK"><div data-test-action="FavoriteToggleClick" class="sc-jlqnSw udMMr"><div class="sc-jjeeWu lnUail"><svg width="24" height="24" viewBox="0 0 24 24"><path fill="#f75059" fill-rule="evenodd" d="M16 3c3.59 0 6 2.41 6 6 0 4.452-3.44 8.308-9.311 11.824-.394.246-.98.246-1.366.006C5.46 17.353 2 13.466 2 9c0-3.59 2.41-6 6-6 1.39 0 2.746.61 4 1.641C13.254 3.61 14.61 3 16 3z"></path></svg></div><div class="sc-jjeeWu dFZxEf"><svg width="24" height="24" viewBox="0 0 24 24" fill="currentColor" class="sc-dOGYXd ikyMDp"><path d="M12.333 5.673L12 5.97l-.333-.298C10.487 4.618 9.229 4 8 4 4.962 4 3 5.962 3 9c0 4.01 3.244 7.656 8.842 10.975a.4.4 0 0 0 .326-.004C17.772 16.615 21 12.996 21 9c0-3.038-1.962-5-5-5-1.229 0-2.488.618-3.667 1.673zM16 3c3.59 0 6 2.41 6 6 0 4.452-3.44 8.308-9.311 11.824-.394.246-.98.246-1.366.006C5.46 17.353 2 13.466 2 9c0-3.59 2.41-6 6-6 1.39 0 2.746.61 4 1.641C13.254 3.61 14.61 3 16 3z"></path></svg></div></div></div></div></div></div><figcaption class="sc-cOxWqc sc-dASMHb sc-cjpfIh eOPaPs jhYtTr jQiGFk"><span data-test-block="ProductName" color="primary" class="sc-cOxWqc sc-fxvcDT bOrVyP dreCvV">Лакосте белые турция</span></figcaption></div></figure></a></span></div></div><div data-test-component="ProductOrAdCard" class="sc-byxFCI ytiGe"><div><span class="sc-llGDqb sc-gqgnwQ fEAASo hZGRky"><a href="/krasnodar/dom-dacha/rasteniya/stolietnik-aloie-6364b4a94b11796f683afe17" rel="noopener noreferrer" style="cursor: pointer;" title="Столетник -Алое"><figure data-test-component="ProductCard" data-test-id="6364b4a94b11796f683afe17" class="sc-hsZwpi fssEAr"><div class="sc-gcHwEF sc-fkxeQW eHsIOL jskOCU"><div class="sc-mxuny kRlvNr"><svg class="sc-dOrDzm uyVta sc-hJMuen ipuCdo" role="img" aria-label=""><image preserveAspectRatio="xMidYMid slice" xlink:href="https://cdn0.youla.io/files/images/360_360/63/64/6364b491af8884580941d4d4-1.jpg" width="100%" height="100%" x="0" y="0"></image></svg></div><div data-test-component="Badges" class="sc-jUOgmM heJjNv"><div class="sc-jcTjWU eYjgWi"><div class="sc-fkMAoN giYGOi"><div><div class="sc-jPNehe kDqmIb"><svg width="16" height="16" viewBox="0 0 16 16" data-test-block="PaymentIcon" class="sc-dmjyfX gLwsSM"><g fill="#FFF" fill-rule="evenodd"><path d="M14 2.8v6.179c0 2.21-1.492 4.164-3.682 4.824L8 14.5l-2.317-.697C3.492 13.143 2 11.19 2 8.98V2.8l6-1.3 6 1.3zm-6.318 7.988v-.447h1.08a.55.55 0 0 0 .363-.112.378.378 0 0 0 .134-.307.385.385 0 0 0-.134-.31.543.543 0 0 0-.364-.114H7.706v-.685h.938c.501 0 .924-.085 1.267-.253.343-.168.601-.404.774-.707a2.11 2.11 0 0 0 .259-1.06c0-.415-.09-.776-.269-1.082A1.784 1.784 0 0 0 9.894 5c-.342-.166-.754-.25-1.235-.25H7.394c-.342 0-.595.08-.76.241-.164.16-.246.41-.246.75v2.066h-.361c-.18.003-.32.05-.42.14a.465.465 0 0 0-.152.365c0 .155.05.277.152.364.1.087.24.132.42.136h.361v.685h-.435a.541.541 0 0 0-.366.114.388.388 0 0 0-.132.31c0 .13.044.232.132.307.088.074.21.112.366.112h.46v.447a.605.605 0 0 0 .222.376.657.657 0 0 0 .412.134c.16 0 .297-.045.413-.134a.605.605 0 0 0 .222-.376z"></path><path d="M8.322 7.808c.442 0 .766-.078.971-.233.205-.156.308-.414.308-.776 0-.359-.098-.62-.293-.781-.195-.162-.487-.243-.874-.243H7.7v2.033h.62z"></path></g></svg></div></div></div><div class="sc-fkMAoN khwkAL"><div><div class="sc-gnGSMD cAHcTq"><svg width="16" height="16" viewBox="0 0 24 24" data-test-block="DeliveryIcon" class="sc-dmjyfX gLwsSM"><path fill="#fff" fill-rule="evenodd" d="M3.393 11.5l.643-3h6.214a.75.75 0 1 0 0-1.5H4.357l.389-1.814A1.5 1.5 0 0 1 6.213 4h8.932a1.5 1.5 0 0 1 1.466 1.814L16.143 8h.15a3 3 0 0 1 2.581 1.472l2.336 3.947a2 2 0 0 1 .258 1.302l-.223 1.562A2 2 0 0 1 19.265 18h-.436a3.001 3.001 0 0 1-5.658 0H13v-.015c-.07.01-.14.015-.213.015H8.83a3.001 3.001 0 0 1-5.72-.2 1.5 1.5 0 0 1-.72-1.614L3.07 13h6.18a.75.75 0 1 0 0-1.5H3.393zM6 18a1 1 0 1 0 0-2 1 1 0 0 0 0 2zm11.686-7.448a2 2 0 0 0-1.704-.952H15.8l-.9 3.9h4.6l-1.814-2.948zM16 18a1 1 0 1 0 0-2 1 1 0 0 0 0 2z"></path></svg><span class="sc-cOxWqc sc-eipBjq jfMydn jXbdGA">Краснодар</span></div></div></div></div></div></div><div class="sc-gKelgU joeTcw"><div class="sc-fNYidB hcfixG"><div class="sc-jRZndg kkuRyV"><p data-test-block="ProductPrice" class="sc-cOxWqc sc-gtBfHm iIywHL kSsyhU"><span data-test-component="Price" class="sc-dbkbXZ izEAeu sc-hZjMBx grkrjO"><span class="sc-dXqfbs iMealh">400</span> <span class="sc-jGoKbf bKjCVH"><span>₽</span><i class="sc-kCuNey irLxOX">руб.</i></span></span></p></div><div role="button" class="sc-kHvPkg jsngjZ"><svg width="40" height="13" viewBox="0 0 40 13" fill="currentColor" class="sc-hEBzJi dTJGgC"><path d="M0 12v-.011A4.003 4.003 0 0 0 3.487 9.29C4.993 3.778 9.845 0 16 0h8c6.155 0 11.007 3.778 12.513 9.29A4.003 4.003 0 0 0 40 11.99V13H0v-1z"></path></svg><svg width="30" height="9" viewBox="0 0 30 9" fill="currentColor" class="sc-dMuQbb fIjttd"><path d="M27.865 8H0v-.004A3.002 3.002 0 0 0 2.67 6h.007c1.407-3.617 4.88-6 9.165-6h6.316c4.285 0 7.758 2.383 9.165 6h.008A3.002 3.002 0 0 0 30 7.996V8h-2.135zM15.088 24h-.18.18zM0 8h30v1H0V8z"></path></svg><div><div class="sc-jPNehe FpavK"><div data-test-action="FavoriteToggleClick" class="sc-jlqnSw udMMr"><div class="sc-jjeeWu lnUail"><svg width="24" height="24" viewBox="0 0 24 24"><path fill="#f75059" fill-rule="evenodd" d="M16 3c3.59 0 6 2.41 6 6 0 4.452-3.44 8.308-9.311 11.824-.394.246-.98.246-1.366.006C5.46 17.353 2 13.466 2 9c0-3.59 2.41-6 6-6 1.39 0 2.746.61 4 1.641C13.254 3.61 14.61 3 16 3z"></path></svg></div><div class="sc-jjeeWu dFZxEf"><svg width="24" height="24" viewBox="0 0 24 24" fill="currentColor" class="sc-dOGYXd ikyMDp"><path d="M12.333 5.673L12 5.97l-.333-.298C10.487 4.618 9.229 4 8 4 4.962 4 3 5.962 3 9c0 4.01 3.244 7.656 8.842 10.975a.4.4 0 0 0 .326-.004C17.772 16.615 21 12.996 21 9c0-3.038-1.962-5-5-5-1.229 0-2.488.618-3.667 1.673zM16 3c3.59 0 6 2.41 6 6 0 4.452-3.44 8.308-9.311 11.824-.394.246-.98.246-1.366.006C5.46 17.353 2 13.466 2 9c0-3.59 2.41-6 6-6 1.39 0 2.746.61 4 1.641C13.254 3.61 14.61 3 16 3z"></path></svg></div></div></div></div></div></div><figcaption class="sc-cOxWqc sc-dASMHb sc-cjpfIh eOPaPs jhYtTr jQiGFk"><span data-test-block="ProductName" color="primary" class="sc-cOxWqc sc-fxvcDT bOrVyP dreCvV">Столетник -Алое</span></figcaption></div></figure></a></span></div></div></div></div>
我不知道如何找到嵌套的 href - /krasnodar/krasota-i-zdorove/parfyumeriya/lakostie-bielyie-turtsiia-63d9233e35c2d94a671b77a0
您需要在整个页面上查找 href,而不仅仅是在此示例中。因为 //div [@data-test-component = 'ProductOrAdCard'] 找到所有元素,但我不明白如何获取位于 href 中的链接。
寻找如此
//div[@data-test-component = 'ProductOrAdCard']
此外,我不明白如何接收属性和链接。因为 span 标签中的类名 (class=“sc-llGDqb, sc-gqgnwQ fEAASo hZGRky”) 是动态的,不需要按类名搜索。我做错了什么?
driver.findElements(By.xpath("//div [@data-test-component = 'ProductOrAdCard']"))
.get(0)
.getAttribute("href");
答:
1赞
Cosmo773
2/12/2023
#1
尝试一下:
List<WebElement> divElements = driver.findElements(By.xpath("//div[@data-test-component='ProductOrAdCard']"));
List<String> hrefs = new ArrayList<>();
for (WebElement div : divElements) {
hrefs.add(div.findElement(By.xpath("//a").getAttribute("href")));
}
或者说:
List<String> hrefs = driver.findElements(By.xpath("//div[@data-test-component='ProductOrAdCard']//a[1]"))
.stream()
.map(element -> element.getAttribute("href"))
.toList();
评论
0赞
Artist
2/12/2023
返回了此类相同链接的列表 trk.mail.ru/c/ds8xq6?mt_sub1=youla.ru&mt_sub5=74 我们需要这些: /krasnodar/krasota-i-zdorove/parfyumeriya/lakostie-bielyie-turtsiia-63d9233e35c2d94a671b77a0
0赞
Artist
2/12/2023
结构如下所示 - ibb.co/ssZsmSx
0赞
Cosmo773
2/12/2023
@Artist,给我html页面的完整代码
0赞
Artist
2/12/2023
youla.ru/?attributes[sort_field]=date_published只能从 //div [@data-test-component = 'ProductOrAdCard'] 收集
1赞
Cosmo773
2/12/2023
在网站上检查,一切正常://div [@data-test-component = 'ProductOrAdCard']//a[1],稍微调整了一下答案。
评论