簡體   English   中英

如何迭代 Python Webbot/Selenium 中的子元素?

[英]How to iterate over children webelements in Python Webbot/Selenium?

我在 Selenium 瀏覽器中有一個搜索結果表,每個搜索結果都在 Html 中定義,如下所示:

<div class="item
            itemWrapper
            ItemPosition1
            ItemMonitor
            " data-position="1" data-it-name="NAME OF THE ITEM" data-it-category="Category" role="article">

    <div class="item-image">

        <a href="/some/link/" target="_blank" rel="noopener" class="itemRec">
            <img src="https://some.jpg" alt="some name" class="img-responsive">
        </a>
                        
    </div>

    <h2 class="small-text item-title">
        <a href="/some/other/link" class="sendItem" target="_blank" rel="noopener">Link Text</a>
    </h2>

    <div class="item-bottom">

        <div class="pull-left item-price">
                <span>999</span>
        </div>

        <div class="pull-right detail-link">
            <a href="/link/to/detail" title="link title" class="detail"
                Detail
            </a>        
        </div>
    </div>
</div>  

我可以通過 classname = item 找到所有 webelements。

elements = driver.find_elements_by_class_name("item")

我需要遍歷元素並獲取它們的 position、名稱和價格,以便能夠單擊其中之一:

for e in elements:
   position=e.get_attribute("data-position").value, 
   name=e.get_attribute("data-it-name").value, 
   price=e.find_element(By.CLASS_NAME,'item-price').value

但這不起作用 - get_attribute 返回 None 並且 find_element 沒有找到任何子元素

您能告訴我如何正確獲取“數據”屬性和子元素值嗎?

使用 Webbot 的整個代碼:

import webbot
from selenium.webdriver.common.by import By
web = webbot.Browser()

web.go_to('www.***.cz')
web.type('bed', classname='header-search-form')
web.press(web.Key.ENTER)
elements = web.find_elements(classname="product-item")
for e in elements:
    name = e.get_attribute("data-it-name").value
    price = e.find_element(By.CLASS_NAME, 'item-price').value
    print(name,price)
    break

classname名在 webbot 中的行為很奇怪。 您肯定沒有在那里獲得產品:

In [56]: elements[0].get_attribute('outerHTML')
Out[56]: '<a href="/">\n\n\t\t\t\t\t\t<img src="https://s.favi.cz/static/frontend/_global/images/favi-logo/favi-logo.60d511aff13247dd52f15acf6bdf2af9.svg" role="banner">\n\n\t\t\t\t\t</a>'

適用於 CSS 選擇器:

In [58]: elements = web.find_elements(css_selector=".product-item")

In [59]: elements[0].get_attribute('outerHTML')
Out[59]: '<div class="\n\t\t\tproduct-item\n\t\t\titemWrapper\n\t\t\tproductItemPosition1\n\t\t\tproductItemMonitor\n\t\t\tproductItemWrapper\n\t\t\tsendProductTransactionWrapper\n\t\t\t\t\t" data-position="1" data-pr-name="Moderní box spring postel Alvares 160x200, bílá" data-tr-id="04d62b60-9d00-4d1b-b03c-2258c50bfdb9" data-pr-category="Postele" data-tr-ob-id="2144583" data-m-ob-id="2345478" role="article">\n\n\t\t<div class="product-image">\n\n\t\t\t<a href="/produkty/nakupovat/2345478/1/84/1/searchBlockSearch/04d6ec5f-0aaf-458e-8754-58f7ccf4e971/04d62b60-9d00-4d1b-b03c-2258c50bfdb9" target="_blank" rel="noopener" class="\n\t\t\t\t\tsendProductTransaction\n\t\t\t\t\tproductRecommendation\t\t\t\t\t\t\t\t\t">\n\t\t\t\t\t\t\t\t\t<img src="https://s.favi.cz/static/images/t/product/300/6f/92/6f922779-bc84-483e-b1cd-ad8522ef0c92.jpg" alt="Moderní box spring postel Alvares 160x200, bílá" class="img-responsive">\n\t\t\t\t\t\t\t</a>\n\n\t\t\t<a href="#" title="Líbí se mi" class="\n\t\t\t\t\tuser-like\n\t\t\t\t\tuserLike\n\t\t\t\t\tproductRecommendation\t\t\t\t\t\t\t\t\t\twith-count\t\t\t\t" data-like-product-master-id="2144583">\n\t\t\t\t\t\t\t\t\t<span class="count">485</span>\n\t\t\t\t\t\t\t</a>\n\n\t\t\t\n\t\t\t\n\t\t</div>\n\n\t\t<div class="product-labels stickers-holder">\n\n\t\t\t\t\t\t\t<span class="sticker storage white">\n\t\t\t\t\t<span class="text">Skladem</span>\n\t\t\t\t</span>\n\t\t\t\n\t\t\t\n\t\t\t\n\t\t\t\n\t\t</div>\n\n\t\t<h2 class="small-text product-item-title">\n\t\t\t<a href="/produkty/nakupovat/2345478/1/84/1/searchBlockSearch/04d6ec5f-0aaf-458e-8754-58f7ccf4e971/04d62b60-9d00-4d1b-b03c-2258c50bfdb9" class="\n\t\t\t\t\tsendProductTransaction\n\t\t\t\t\tproductRecommendation\t\t\t\t" target="_blank" rel="noopener">Moderní box spring postel Alvares 160x200, bílá</a>\n\t\t</h2>\n\n\t\t<div class="product-bottom">\n\n\t\t\t<div class="pull-left product-item-price">\n\t\t\t\t<span>15 599 Kč</span>\n\t\t\t\t\t\t\t</div>\n\n\t\t\t<div class="pull-right product-shop-link">\n\t\t\t\t<a href="/produkty/p/luxusni-box-spring-postel-alves-160x200-bila?v=2345478" title="Moderní box spring postel Alvares 160x200, bílá" class="btn btn-bordered btn-sm sendProductTransaction">\n\t\t\t\t\tDetail\n\t\t\t\t</a>\n\n\t\t\t\t<a href="/produkty/nakupovat/2345478/1/84/1/searchBlockSearch/04d6ec5f-0aaf-458e-8754-58f7ccf4e971/04d62b60-9d00-4d1b-b03c-2258c50bfdb9" target="_blank" rel="noopener" class="\n\t\t\t\t\t\tbtn\n\t\t\t\t\t\tbtn-std\n\t\t\t\t\t\tbtn-sm\n\t\t\t\t\t\tproductRecommendation\t\t\t\t\t\tsendProductTransaction\n\t\t\t\t\t">\n\t\t\t\t\t<strong>Do obchodu</strong>\n\t\t\t\t</a>\n\t\t\t</div>\n\n\t\t</div>\n\n\t\t\n\t</div>'

In [60]: elements[0].get_attribute('data-position')
Out[60]: '1'

In [61]: elements[0].get_attribute('data-pr-name')
Out[61]: 'Moderní box spring postel Alvares 160x200, bílá'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM