【问题标题】:How to specify clicking a class when there are multiple with the same class name. (Selenium Python)当有多个具有相同类名的类时如何指定单击一个类。 (硒蟒)
【发布时间】:2021-01-01 14:45:28
【问题描述】:
<li class="mod-tile">
<ul class="gallery-hidden gallery">
<li class="thumb" data-src="https://staticdelivery.nexusmods.com/mods/2531/images/1982/1982-1579465585-271404739.png" data-sub-html="Stracker's Loader " data-exthumbimage="https://staticdelivery.nexusmods.com/mods/2531/images/thumbnails/1982/1982-1579465585-271404739.png" />
</ul>
<div data-mod-id="1982" data-game-id="2531" class="mod-tile-left ">
<div class="expandtile">
<ul class="btnexpand btnoverlay inline-flex">
<div class="padding"></div>
<svg title="" class="icon-plus"><use xlink:href="https://www.nexusmods.com/assets/images/icons/icons.svg#icon-plus"></use></svg> <li>
<ul>
<li><a class="mod-view" href="index.html">View mod page</a></li>
<li><a class="mod-gallery" href="index.html">View image gallery</a></li>
</ul>
</li>
</ul>
</div>
<a class="mod-image" href="index.html">
<figure class="image_figure">
<img class="back" src="https://www.nexusmods.com/assets/images/default/noimage.svg" width="600" height="338">
<div class="fore_div_mods">
<img class="fore" onerror="imgError(this,'https://www.nexusmods.com/assets/images/default/noimage.svg')" loading="lazy" src="https://staticdelivery.nexusmods.com/mods/2531/images/thumbnails/1982/1982-1579465585-271404739.png" alt="Stracker's Loader" title="Stracker's Loader">
</div>
</figure>
</a>
<div class="tile-desc motm-tile">
<div class="fadeoff"></div>
<div class="tile-content">
<h3><a href="index.html">Stracker's Loader</a></h3>
<div class="meta clearfix">
<div class="category">
<a href="index.html">Utilities</a>
</div>
<time class="date" datetime="2020-01-19 20:26"> <span class="label">Uploaded: </span>
19 Jan 2020 </time>
<div class="date"><span class="label">Last Update:</span> 04 Dec 2020</div>
<div class="realauthor"><span class="label">Author: </span> Stracker</div>
<div class="author"><span class="label">Uploader: </span> <a href="index.html">Stracker</a></div>
</div>
<p class="desc">
Restores full nativePC functionality. </p>
</div>
</div>
<div class="tile-data">
<ul class="clearfix">
<li class="sizecount inline-flex">
<svg title="" class="icon icon-filesize"><use xlink:href="https://www.nexusmods.com/assets/images/icons/icons.svg#icon-filesize"></use></svg> <span class="flex-label">
703KB </span>
</li>
<li class="endorsecount inline-flex">
<svg title="" class="icon icon-endorse"><use xlink:href="https://www.nexusmods.com/assets/images/icons/icons.svg#icon-endorse"></use></svg> <span class="flex-label">26.0k</span>
</li>
<li class="downloadcount inline-flex">
<svg title="" class="icon icon-downloads"><use xlink:href="https://www.nexusmods.com/assets/images/icons/icons.svg#icon-downloads"></use></svg> <span class="flex-label"> -- </span>
</li>
</ul>
</div>
</div>
<div class="mod-tile-right">
<div class="tile-desc">
<div class="fadeoff"></div>
<div class="tile-content">
<h3><a href="index.html">Stracker's Loader</a></h3>
<div class="meta clearfix">
<div class="category">
<a href="index.html">Utilities</a>
</div>
<time class="date" datetime="2020-01-19 20:26"> <span class="label">Uploaded: </span>
19 Jan 2020 </time>
<div class="date"><span class="label">Last Update:</span> 04 Dec 2020</div>
<div class="author"><span class="label">Author: </span><a href="index.html">Stracker</a></div>
</div>
<p class="desc">
Restores full nativePC functionality. </p>
</div>
</div>
</div>
</li>
<li class="mod-tile">
<ul class="gallery-hidden gallery">
<li class="thumb" data-src="https://staticdelivery.nexusmods.com/mods/2531/images/112/112-1579010242-745113274.png" data-sub-html="Souvenir's Light Pillar " data-exthumbimage="https://staticdelivery.nexusmods.com/mods/2531/images/thumbnails/112/112-1579010242-745113274.png" />
<li class="thumb" data-src="https://staticdelivery.nexusmods.com/mods/2531/images/112/112-1579010135-1239485031.png" data-sub-html="Souvenir's Light Pillar " data-exthumbimage="https://staticdelivery.nexusmods.com/mods/2531/images/thumbnails/112/112-1579010135-1239485031.png" />
<li class="thumb" data-src="https://staticdelivery.nexusmods.com/mods/2531/images/112/112-1579010181-399571475.png" data-sub-html="Souvenir's Light Pillar " data-exthumbimage="https://staticdelivery.nexusmods.com/mods/2531/images/thumbnails/112/112-1579010181-399571475.png" />
<li class="thumb" data-src="https://staticdelivery.nexusmods.com/mods/2531/images/112/112-1579259504-344088346.png" data-sub-html="Souvenir's Light Pillar " data-exthumbimage="https://staticdelivery.nexusmods.com/mods/2531/images/thumbnails/112/112-1579259504-344088346.png" />
</ul>
<div data-mod-id="112" data-game-id="2531" class="mod-tile-left ">
<div class="expandtile">
<ul class="btnexpand btnoverlay inline-flex">
<div class="padding"></div>
<svg title="" class="icon-plus"><use xlink:href="https://www.nexusmods.com/assets/images/icons/icons.svg#icon-plus"></use></svg> <li>
<ul>
<li><a class="mod-view" href="index.html">View mod page</a></li>
<li><a class="mod-gallery" href="index.html">View image gallery</a></li>
</ul>
</li>
</ul>
</div>
<a class="mod-image" href="index.html">
<figure class="image_figure">
<img class="back" src="https://www.nexusmods.com/assets/images/default/noimage.svg" width="600" height="338">
<div class="fore_div_mods">
<img class="fore" onerror="imgError(this,'https://www.nexusmods.com/assets/images/default/noimage.svg')" loading="lazy" src="https://staticdelivery.nexusmods.com/mods/2531/images/thumbnails/112/112-1579010242-745113274.png" alt="Souvenir's Light Pillar" title="Souvenir's Light Pillar">
</div>
</figure>
</a>
<div class="tile-desc motm-tile">
<div class="fadeoff"></div>
<div class="tile-content">
<h3><a href="index.html">Souvenir's Light Pillar</a></h3>
<div class="meta clearfix">
<div class="category">
<a href="index.html">Visuals and Graphics</a>
</div>
<time class="date" datetime="2018-09-03 19:58"> <span class="label">Uploaded: </span>
03 Sep 2018 </time>
<div class="date"><span class="label">Last Update:</span> 17 Jan 2020</div>
<div class="realauthor"><span class="label">Author: </span> 2hh8899</div>
<div class="author"><span class="label">Uploader: </span> <a href="index.html">2hh8899</a></div>
</div>
<p class="desc">
It lights up the souvenirs for making them easier to find.유실물을 찾기 쉽게 하기 위해 빛기둥을 박아넣었습니다. </p>
</div>
</div>
<div class="tile-data">
<ul class="clearfix">
<li class="sizecount inline-flex">
<svg title="" class="icon icon-filesize"><use xlink:href="https://www.nexusmods.com/assets/images/icons/icons.svg#icon-filesize"></use></svg> <span class="flex-label">
59KB </span>
</li>
<li class="endorsecount inline-flex">
<svg title="" class="icon icon-endorse"><use xlink:href="https://www.nexusmods.com/assets/images/icons/icons.svg#icon-endorse"></use></svg> <span class="flex-label">22.7k</span>
</li>
<li class="downloadcount inline-flex">
<svg title="" class="icon icon-downloads"><use xlink:href="https://www.nexusmods.com/assets/images/icons/icons.svg#icon-downloads"></use></svg> <span class="flex-label"> -- </span>
</li>
</ul>
</div>
</div>
<div class="mod-tile-right">
<div class="tile-desc">
<div class="fadeoff"></div>
<div class="tile-content">
<h3><a href="index.html">Souvenir's Light Pillar</a></h3>
<div class="meta clearfix">
<div class="category">
<a href="index.html">Visuals and Graphics</a>
</div>
<time class="date" datetime="2018-09-03 19:58"> <span class="label">Uploaded: </span>
03 Sep 2018 </time>
<div class="date"><span class="label">Last Update:</span> 17 Jan 2020</div>
<div class="author"><span class="label">Author: </span><a href="index.html">2hh8899</a></div>
</div>
<p class="desc">
It lights up the souvenirs for making them easier to find.유실물을 찾기 쉽게 하기 위해 빛기둥을 박아넣었습니다. </p>
</div>
</div>
</div>
</li>
<li class="mod-tile">
<ul class="gallery-hidden gallery">
<li class="thumb" data-src="https://staticdelivery.nexusmods.com/mods/2531/images/43/43-1534824818-145235267.png" data-sub-html="MHW Transmog " data-exthumbimage="https://staticdelivery.nexusmods.com/mods/2531/images/thumbnails/43/43-1534824818-145235267.png" />
<li class="thumb" data-src="https://staticdelivery.nexusmods.com/mods/2531/images/43/43-1534825195-804021906.png" data-sub-html="MHW Transmog " data-exthumbimage="https://staticdelivery.nexusmods.com/mods/2531/images/thumbnails/43/43-1534825195-804021906.png" />
</ul>
<div data-mod-id="43" data-game-id="2531" class="mod-tile-left ">
<div class="expandtile">
<ul class="btnexpand btnoverlay inline-flex">
<div class="padding"></div>
<svg title="" class="icon-plus"><use xlink:href="https://www.nexusmods.com/assets/images/icons/icons.svg#icon-plus"></use></svg> <li>
<ul>
<li><a class="mod-view" href="index.html">View mod page</a></li>
<li><a class="mod-gallery" href="index.html">View image gallery</a></li>
</ul>
</li>
</ul>
</div>
<a class="mod-image" href="index.html">
<figure class="image_figure">
<img class="back" src="https://www.nexusmods.com/assets/images/default/noimage.svg" width="600" height="338">
<div class="fore_div_mods">
<img class="fore" onerror="imgError(this,'https://www.nexusmods.com/assets/images/default/noimage.svg')" loading="lazy" src="https://staticdelivery.nexusmods.com/mods/2531/images/thumbnails/43/43-1534824818-145235267.png" alt="MHW Transmog" title="MHW Transmog">
</div>
</figure>
</a>
<div class="tile-desc motm-tile">
<div class="fadeoff"></div>
<div class="tile-content">
<h3><a href="index.html">MHW Transmog</a></h3>
<div class="meta clearfix">
<div class="category">
<a href="index.html">Utilities</a>
</div>
<time class="date" datetime="2018-08-21 05:37"> <span class="label">Uploaded: </span>
21 Aug 2018 </time>
<div class="date"><span class="label">Last Update:</span> 04 Dec 2020</div>
<div class="realauthor"><span class="label">Author: </span> Approved</div>
<div class="author"><span class="label">Uploader: </span> <a href="index.html">FineNerds</a></div>
</div>
<p class="desc">
A mod that allows you to hot swap your appearance with any armor of your choice. Visible to other players!As with any mod for games that don't support mods. This is USE AT YOUR OWN RISK. </p>
</div>
</div>
<div class="tile-data">
<ul class="clearfix">
<li class="sizecount inline-flex">
<svg title="" class="icon icon-filesize"><use xlink:href="https://www.nexusmods.com/assets/images/icons/icons.svg#icon-filesize"></use></svg> <span class="flex-label">
260KB </span>
</li>
<li class="endorsecount inline-flex">
<svg title="" class="icon icon-endorse"><use xlink:href="https://www.nexusmods.com/assets/images/icons/icons.svg#icon-endorse"></use></svg> <span class="flex-label">12.2k</span>
</li>
<li class="downloadcount inline-flex">
<svg title="" class="icon icon-downloads"><use xlink:href="https://www.nexusmods.com/assets/images/icons/icons.svg#icon-downloads"></use></svg> <span class="flex-label"> -- </span>
</li>
</ul>
</div>
</div>
<div class="mod-tile-right">
<div class="tile-desc">
<div class="fadeoff"></div>
<div class="tile-content">
<h3><a href="index.html">MHW Transmog</a></h3>
<div class="meta clearfix">
<div class="category">
<a href="index.html">Utilities</a>
</div>
<time class="date" datetime="2018-08-21 05:37"> <span class="label">Uploaded: </span>
21 Aug 2018 </time>
<div class="date"><span class="label">Last Update:</span> 04 Dec 2020</div>
<div class="author"><span class="label">Author: </span><a href="index.html">FineNerds</a></div>
</div>
<p class="desc">
A mod that allows you to hot swap your appearance with any armor of your choice. Visible to other players!As with any mod for games that don't support mods. This is USE AT YOUR OWN RISK. </p>
</div>
</div>
</div>
</li>

所以我希望 selenium 打开具有“mod-tile”类的第一个链接,然后它会执行我制作的脚本,然后我希望它打开下一个链接具有相同的类“mod-tile”。有没有办法指定这个? (顺便说一句,不要介意我刚刚复制了网站上出现的前 3 个模块图块的描述)

【问题讨论】:

    标签: python selenium-webdriver


    【解决方案1】:

    您可以使用 find_elements_by_class_name() 方法获取元素列表。 然后遍历元素列表。

    [更新] 以下应单击“mod-tile”元素中的第一个链接(//a)

    elements = driver.find_elements_by_class_name("mod-tile")
    print("mod-title count {}".format(len(elements)))
    
    for element in range(len(elements)):
      elements[element].find_element_by_xpath("//a").click()
    

    【讨论】:

    • 抱歉,我是 selenium 新手,如何遍历元素?
    • 示例代码演示了遍历元素。第一行获取具有类名的元素列表。然后 for 循环遍历元素列表。
    • 将代码粘贴到我的脚本中,它只打开 1 个链接,而且它似乎没有运行我拥有的其余代码(查找并单击文件 -> 查找并单击手动下载)跨度>
    • 代码已更新为单击每个 mod-tile 部分的第一个链接。添加了 print 语句以检查带有 mod-tile 的元素。这应该不止一个。否则我们需要进一步调查。
    • 更新后的代码对我不起作用,它将标签发送到 nexusmods.com 基础站点而不是 mods,pastebin link + github for code at bottom
    猜你喜欢
    • 1970-01-01
    • 2022-01-18
    • 2015-01-05
    • 1970-01-01
    • 1970-01-01
    • 2019-10-01
    • 2022-07-06
    • 1970-01-01
    • 2021-06-02
    相关资源
    最近更新 更多