GigaByte
GigaByte

Reputation: 700

How can i extract href from this html using selenium?

I want to extract href from the following html using selenium:

   <div class="resultsContainer clearfix" data-index="0"> 
        <div class="tableItem eventType">
            <span class="tileNumber">1.&nbsp;</span>         
            <span class="miles">0 mi</span>      
        </div>   
        <div class="tableItem eventTime">
            <span class="tileNumber">1.&nbsp;</span> 
                <ul>         
                    <li class="placeName clearfix">
                        <a class="placeName" href="/en/home/play/facility-listing/facility-details.html?facilityId=777EMBARCADEROROAD-CA-94301">Rinconada Park</a></li>    
                    <li class="timeNumber phoneNumber">(650) 463-4900</li>        
                </ul>       
        </div>       
        <div class="tableItem eventSize">     
            <ul>       
                <li>
                    <span class="eventNameType"># of Courts:</span>                     
                    <span class="eventNameValue">9</span>
                </li>          
                <li>
                    <span class="eventNameType">Court Size:</span> 
                    <span class="eventNameValue"> 36', 60', 78'</span>
                </li>
                <li>
                    <span class="eventNameType">Indoor Courts:</span>
                    <span class="eventNameValue">No</span>
                 </li>         
            </ul>       
        </div>       

        <div class="tableItem eventLocation">         
            <ul>           
                <li class="mileValue">0 mi</li>           
                <li>777 Embarcadero Road</li>           
                <li>Palo Alto, California, 94301</li>         
            </ul>       
        </div>       

        <div class="tableItem eventMoreInfo">         
            <div class="seeMoreBtn">           
                <a class="btn primaryBtn" href="/en/home/play/facility-listing/facility-details.html?facilityId=777EMBARCADEROROAD-CA-94301">MORE INFO</a>         
            </div>       
        </div>     
    </div>

The selenium(Python) code that i'm using is as follows:

import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains


# ----------------------------------------------HANDLING-SELENIUM-STUFF-------------------------------------------------

driver = webdriver.Chrome()
time.sleep(5)
driver.get('https://www.usta.com/en/home/play/facility-listing.html?searchTerm=&distance=5000000000&address=Palo%20Alto,%20%20CA')
wait = WebDriverWait(driver,5)
time.sleep(5)
links = driver.find_element_by_xpath("//div[@class='seeMoreBtn']/a")
print(links.text)
time.sleep(3)
for link in links:
    print(link.get_attribute("href"))
driver.close()

Following is the webpage from where i want to extract the links using the xpath above(more precisely links under MoreInfo button section)

On executing the above code i'm getting this error:

Traceback (most recent call last):
  File "E:/Python/CSV/scraperscrapero.py", line 23, in <module>
    for link in links:
TypeError: 'WebElement' object is not iterable

But the above XPATH on this page contains 5 Buttons of MoreInfo.

I just wanted to get those five links which are under a tag of Button MoreInfo

Upvotes: 0

Views: 2253

Answers (1)

Subburaj
Subburaj

Reputation: 2334

You have used find_element instead of find_elements.

Please change the below line and then check

links = driver.find_elements_by_xpath("//div[@class='seeMoreBtn']/a")

Upvotes: 3

Related Questions