0

Below is the html code where i want to extract the text "Page 2 of 2" from it

HTML code

<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>

below is my code attempt

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")

the above code is working but it is extracting the text of another one before this HTML code please help on getting the text page 2 of 2!!

2 Answers 2

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

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

If there are two elements index the second one.

Also when indexing from parent use a .// otherwise you'll get from root.

Sign up to request clarification or add additional context in comments.

3 Comments

im getting the below error AttributeError: 'list' object has no attribute 'get_attribute' ```
Try the updated one. Might have to use 1 if it only finds one. Or just use find_element.
This worked with a 1 instead of 2 elem = driver.find_elements_by_xpath("//span[@class='hide page-summary']") print(elem[1].get_attribute("textContent")) Thank You!!!!
0

Use the .text

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

5 Comments

when i run the above code i get a No such element exception
try 'hide.page-summary', it sometimes works if you don't have any spaces in the class name @ArjunMunirathinam
try this - driver.find_element_by_xpath("//span[@class='hide page-summary']")
add some time.sleep(3) as well, reason could be that selenium is looking for an element that hasn't finished loading yet @ArjunMunirathinam
returns a blank space when i run the below element = driver.find_element_by_xpath("//span[@class='hide page-summary']").text print(element)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.