【问题标题】:How to iterate over a table and print the results from the first 10 rows with python and selenium webdriver?如何使用 python 和 selenium webdriver 遍历表并打印前 10 行的结果?
【发布时间】:2019-02-21 13:51:58
【问题描述】:

使用 selenium webdriver 和 python,我可以找到搜索单元格并搜索返回结果,但是我想打印返回的前 10 行的结果(减去标题行)。

我使用的网站是:http://www.hoovers.com/company-information/company-search.html?term=simon,例如作为搜索词。

我已经搜索了一段时间并尝试了很多东西,包括 xpaths 和大多数错误。这是我迄今为止最接近的一次:

for row in mydriver.find_elements_by_class_name('cmp-company-directory'):
        cell = row.find_elements_by_tag_name("td")[0]
        print(cell.text)

但是它只返回第一行并且不会遍历表。有小费吗? 蒂亚!

【问题讨论】:

    标签: python selenium selenium-webdriver xpath css-selectors


    【解决方案1】:

    在 Xpath 下试试这个,它将遍历表格并打印前 10 行。

    elements=driver.find_elements_by_xpath("//div[@class='clear data-table sortable-header dashed-table-tr alternate-rows']//tr/td")
    counter=1
    for element in elements:
        print(element.text)
        counter+=1
        if counter==50:
            break
    

    输出:

    Simon Property Group, Inc.
    Indianapolis, IN, United States
    $5538.64M
    See Details
    
    SIMON & SCHUSTER (UK) LIMITED
    London, London, England
    $60.39M
    See Details
    
    SIMON JERSEY GROUP LIMITED
    Accrington, Lancashire, England
    
    See Details
    
    Simon Worldwide, Inc.
    Irvine, CA, United States
    $0.0M
    See Details
    
    Simon Property Group, L.P.
    Indianapolis, IN, United States
    $5538.64M
    See Details
    
    Günter Simon e.K. Inh. Carmen Simon
    Ravensburg, Baden-Württemberg, Germany
    
    See Details
    
    Simon e Simon Servicos Odontologicos Ltda
    Vere, Parana, Brazil
    
    See Details
    
    Simon Comercial e Industrial Ltda Em Recuperacao Judicial
    Aparecida De Goiania, Goias, Brazil
    
    See Details
    
    Simon Levelt B.V.
    Haarlem, Noord-Holland, The Netherlands
    
    See Details
    
    SIMON SAU
    Barcelona, Barcelona, Spain
    $115.95M
    See Details
    

    如果您只想打印公司名称的前 10 行,试试这个。

    elements=driver.find_elements_by_xpath("//div[@class='clear data-table sortable-header dashed-table-tr alternate-rows']//tr/td[@class='company_name']")
    counter=0
    for element in elements:
        print(element.text)
        counter+=1
        if counter==10:
            break
    

    输出:-

    Simon Property Group, Inc.
    SIMON & SCHUSTER (UK) LIMITED
    SIMON JERSEY GROUP LIMITED
    Simon Worldwide, Inc.
    Simon Property Group, L.P.
    Günter Simon e.K. Inh. Carmen Simon
    Simon e Simon Servicos Odontologicos Ltda
    Simon Comercial e Industrial Ltda Em Recuperacao Judicial
    Simon Levelt B.V.
    

    让我知道这是否适合你。

    【讨论】:

      【解决方案2】:

      要打印除标题行之外的公司名称,您必须为visibility_of_all_elements_located 引入WebDriverWait,您可以使用以下任一解决方案:

      • CSS_SELECTOR:

        print([company_name.get_attribute("innerHTML") for company_name in WebDriverWait(driver, 10).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "div.cmp-company-directory table td.company_name>a")))])
        
      • XPATH:

        print([company_name.get_attribute("innerHTML") for company_name in WebDriverWait(driver, 10).until(EC.visibility_of_all_elements_located((By.XPATH, "//div[@class='cmp-company-directory']//table//td[@class='company_name']/a")))])
        

      要打印除标题行之外的前 10 个公司名称,您必须为 visibility_of_all_elements_located 诱导 WebDriverWait,然后您必须使用 [:10] 将列表限制为 10 个元素,您可以使用以下任一解决方案:

      • CSS_SELECTOR:

        print([company_name.text for company_name in WebDriverWait(driver, 10).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "div.cmp-company-directory table td.company_name>a")))[:10]])
        
      • XPATH:

        print([company_name.text for company_name in WebDriverWait(driver, 10).until(EC.visibility_of_all_elements_located((By.XPATH, "//div[@class='cmp-company-directory']//table//td[@class='company_name']/a")))[:10]])
        

      注意:您必须添加以下导入:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-10-27
        • 2019-11-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-08-16
        • 1970-01-01
        相关资源
        最近更新 更多