簡體   English   中英

如何使用 selenium python 從下面的 HTML 獲取文本屬性

[英]how to get the text attribute from the below HTML using selenium python

下面是 html 代碼,我想從中提取文本“Page 2 of 2”

HTML代碼

<thead>
        <tr>
            <th scope="col" class="GridHeader_Sonetto"><input id="ctl00_ctl00_ContentPlaceHolderContent_MainCategoriserContent_Map1_SubgroupsAndProducts1_plcCategoryProductsGrid_ctl00_ctl00_ctl02_ctl01_SelectSelectCheckBox" type="checkbox" name="ctl00$ctl00$ContentPlaceHolderContent$MainCategoriserContent$Map1$SubgroupsAndProducts1$plcCategoryProductsGrid$ctl00$ctl00$ctl02$ctl01$SelectSelectCheckBox" onclick="$find(&quot;ctl00_ctl00_ContentPlaceHolderContent_MainCategoriserContent_Map1_SubgroupsAndProducts1_plcCategoryProductsGrid_ctl00&quot;)._selectAllRows(&quot;ctl00$ctl00$ContentPlaceHolderContent$MainCategoriserContent$Map1$SubgroupsAndProducts1$plcCategoryProductsGrid$ctl00$ctl00&quot;, &quot;&quot;, event);setTimeout(&#39;__doPostBack(\&#39;ctl00$ctl00$ContentPlaceHolderContent$MainCategoriserContent$Map1$SubgroupsAndProducts1$plcCategoryProductsGrid$ctl00$ctl00$ctl02$ctl01$SelectSelectCheckBox\&#39;,\&#39;\&#39;)&#39;, 0)" /></th><th scope="col" class="GridHeader_Sonetto" style="display:none;">_InternalID</th><th scope="col" class="GridHeader_Sonetto" style="display:none;">_ID</th><th scope="col" class="GridHeader_Sonetto" style="display:none;">_Name</th><th scope="col" class="GridHeader_Sonetto" style="display:none;">_Order</th><th scope="col" class="GridHeader_Sonetto" style="display:none;">_Source</th><th scope="col" class="GridHeader_Sonetto" style="display:none;">_RootConcept</th><th scope="col" class="GridHeader_Sonetto">TPNB</th><th scope="col" class="GridHeader_Sonetto">Product Name</th>
        </tr>
    </thead><tfoot>
        <tr class="GridPager_Sonetto">
            <td colspan="3"><div class="PagerLeft_Sonetto">
                <span class="items-summary">Items 11 - 15 of 15</span><span class="grid-pages"><span><input type="submit" name="ctl00$ctl00$ContentPlaceHolderContent$MainCategoriserContent$Map1$SubgroupsAndProducts1$plcCategoryProductsGrid$ctl00$ctl00$ctl03$ctl01$ctl02" value=" " title="Previous Page" class="rgPagePrev" /></span>&nbsp;<input type="submit" name="ctl00$ctl00$ContentPlaceHolderContent$MainCategoriserContent$Map1$SubgroupsAndProducts1$plcCategoryProductsGrid$ctl00$ctl00$ctl03$ctl01$ctl03" value=" " onclick="return false;" title="Next Page" class="rgPageNext" />
            </div><div class="PagerRight_Sonetto">
                </span><span class="hide page-summary">Page 2 of 2</span>
            </div></td>
        </tr>
    </tfoot><tbody>

下面是我的代碼嘗試

urll = driver.find_element(By.XPATH, "//input[@id='ctl00_ctl00_ContentPlaceHolderContent_MainCategoriserContent_Map1_SubgroupsAndProducts1_plcCategoryProductsGrid_ctl00_ctl00_ctl02_ctl01_SelectSelectCheckBox']")
            urll.find_element(By.XPATH,"//span[@class='hide page-summary']").get_attribute("textContent")

上面的代碼正在工作,但它正在提取此 HTML 代碼之前的另一個文本,請幫助獲取文本第 2 頁,共 2 頁!

elem=driver.find_elements_by_xpath("//span[@class='hide page-summary']")

print(elem[2].get_attribute("textContent"))

如果有兩個元素索引第二個元素。

此外,當從父級索引時使用 a.// 否則您將從根目錄獲取。

使用.text

element = driver.find_element_by_class_name('hide page-summary').text
print(element)

暫無
暫無

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

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