CoderShresth
CoderShresth

Reputation: 63

How to get data from multiple elements of multiple classes in selenium?

As written in the question, there are multiple h3 tags and multiple buttons some with same classes while some with different, I want to get the text of h3 tags with class companyname and I want to get text of all the buttons with either this class btn btn-xs btn-success or this class btn btn-xs btn-danger

Here is the html:

<div class="col-md-6 col-sm-9">
                    <!-- <h3>Did you mean: <a href="/trademarks/search/"></a></h3> -->

                            <div class="searchresult" style="padding-left: 20px;">
                                <!-- <table> -->
                                    <!-- <tr> -->
                                        <!-- <td width="70%"> -->
                                        <div class="row">
                                          <div class="col-sm-12">
                                            <h3 style="margin-bottom: 0; letter-spacing: 0px;" class="companyname">
                                              <a href="/trademarks/953603-susox" style="">
                                                Susox
                                              </a>
                                            </h3> 

                                            <div class="lighter" style="padding-bottom: 5px">ID: 953603</div>

                                            <p>

                                                  <button type="button" class="btn btn-xs btn-success">Registered</button>


                                            <button type="button" class="btn btn-xs btn-default">Class: 5</button>
                                            <button type="button" class="btn btn-xs btn-default">Device</button>

                                            </p>
                                        </div>

                                        <div class="col-sm-8">
                                            <ul>
                                                <li>Filed on <strong>05 September 2000</strong></li>
                                                <li>Registered in <strong>GUJARAT</strong></li>
                                                <li>Filed in IPOffice <strong>AHMEDABAD</strong></li>
                                                <li>Published in Journal #9999</li>
                                            </ul>
                                        </div>
                                        <div class="col-sm-4 text-center">
                                              <div style="padding: 0px 0 10px 0; border: solid 0px #CCC;">
                                                <img class="img-responsive" alt="Susox" style="max-height: 100px; max-width: 100%" src="https://qcimages.sgp1.digitaloceanspaces.com/store/trademark/7081340/imageshrine/small-1ec5cfa763b881ffa0642b7f6ef802bf.jpg?X-Amz-Algorithm=AWS4-HMAC-SHA256&amp;X-Amz-Credential=GQGLOPQAZ7XLVOTEXYZH%2F20210517%2Fsgp1%2Fs3%2Faws4_request&amp;X-Amz-Date=20210517T094210Z&amp;X-Amz-Expires=900&amp;X-Amz-SignedHeaders=host&amp;X-Amz-Signature=fda1ed68cc66088d64d8cf166341905a61ccc1fac868781b75c8ab78828c587f">
                                              </div>
                                        </div>
                                      </div>
                            </div>
                            <div class="searchresult" style="padding-left: 20px;">
                                <!-- <table> -->
                                    <!-- <tr> -->
                                        <!-- <td width="70%"> -->
                                        <div class="row">
                                          <div class="col-sm-12">
                                            <h3 style="margin-bottom: 0; letter-spacing: 0px;" class="companyname">
                                              <a href="/trademarks/1190452-susox-t2" style="">
                                                Susox T2
                                              </a>
                                            </h3> 

                                            <div class="lighter" style="padding-bottom: 5px">ID: 1190452</div>

                                            <p>

                                                  <button type="button" class="btn btn-xs btn-success">Abandoned</button>


                                            <button type="button" class="btn btn-xs btn-default">Class: 5</button>
                                            <button type="button" class="btn btn-xs btn-default">Word</button>

                                            </p>
                                        </div>

                                        <div class="col-sm-8">
                                            <ul>
                                                
                                                <li>Registered in <strong>HARYANA</strong></li>
                                                <li>Filed in IPOffice <strong>DELHI</strong></li>
                                                
                                            </ul>
                                        </div>
                                        <div class="col-sm-4 text-center">
                                        </div>
                                      </div>
                            </div>
                            <div class="searchresult" style="padding-left: 20px;">
                                <!-- <table> -->
                                    <!-- <tr> -->
                                        <!-- <td width="70%"> -->
                                        <div class="row">
                                          <div class="col-sm-12">
                                            <h3 style="margin-bottom: 0; letter-spacing: 0px;" class="companyname">
                                              <a href="/trademarks/1190450-susox-200" style="">
                                                Susox 200
                                              </a>
                                            </h3> 

                                            <div class="lighter" style="padding-bottom: 5px">ID: 1190450</div>

                                            <p>

                                                  <button type="button" class="btn btn-xs btn-success">Abandoned</button>


                                            <button type="button" class="btn btn-xs btn-default">Class: 5</button>
                                            <button type="button" class="btn btn-xs btn-default">Word</button>

                                            </p>
                                        </div>

                                        <div class="col-sm-8">
                                            <ul>
                                                
                                                <li>Registered in <strong>HARYANA</strong></li>
                                                <li>Filed in IPOffice <strong>DELHI</strong></li>
                                                
                                            </ul>
                                        </div>
                                        <div class="col-sm-4 text-center">
                                        </div>
                                      </div>
                            </div>


                        
                       
 



            </div>

XPATHS:

H3's A tag: //h3[@class='companyname']

Button: //div[@class='col-sm-12']/p/button[@class='btn btn-xs btn-danger'] or: //div[@class='col-sm-12']/p/button[@class='btn btn-xs btn-success']

Upvotes: 2

Views: 285

Answers (2)

Emmanuel Arias
Emmanuel Arias

Reputation: 484

You can find elements by tags (if you want the look for all <h3> tags or by class name. For the first one, you should use:

elements = driver.find_elements_by_tag_name('h3')

for element in elements:
    print(element.text)

or find by class name:

element = driver.find_elements_by_class_name('btn btn-xs btn-success')

for element in elements:
    print(element.text)

Take account that you also have methods named find_element_by_<class_name|tag_name> that only return one element (the first that it found)

Upvotes: 4

Methmal Godage
Methmal Godage

Reputation: 1106

You can get this to an elements list by using 'find_elements'. Then iterate it using a loop and inside the loop place the code to getText(). Then put it into a list and you can get all the texts from that list when you need to use it.

For example:

elements = driver.find_elements(By.ID, 'abc')

for e in elements:
    print(e.text)

Here it prints the text. Instead of that you can add it to a list and return the list from that.

Upvotes: 2

Related Questions