我如何使用硒从该HTML中提取href?

时间:2018-06-29 06:33:58

标签: python-3.x selenium selenium-webdriver selenium-chromedriver

我想使用硒从以下html提取href:

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

我正在使用的Selenium(Python)代码如下:

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)

在执行上述代码时,我收到此错误:

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

但是此页面上的以上 XPATH 包含5个 MoreInfo 按钮。

我只想获取Button MoreInfo标签下的五个链接

1 个答案:

答案 0 :(得分:2)

您使用的是find_element而不是find_elements

请更改以下行,然后检查

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