Selenium和Python的循环

时间:2017-04-13 17:43:08

标签: python loops selenium selenium-chromedriver openpyxl

首先,我是编程的新手,但到目前为止我的代码已经工作了,但是想弄清楚如何自动化它,所以我不一定要更新我的python代码,并一遍又一遍地启动程序。

程序使用某些搜索参数擦除一年内的“计划权限数据”,然后使用openpyxl将其写入excel文件。

我想做的是---

  1. 自动搜索10年(仍需逐年搜索)
  2. 每年都在excel的不同标签上。
  3. 如果您对这两个问题都有任何帮助,我们将不胜感激。试图摆脱这个问题不必要的大部分垃圾代码。

    这是我的程序代码

    python3 UrlScript.py    
    
    Url to grab information off of
    mainPage = "http://edevelopment.falkirk.gov.uk/online/search.do?action=advanced"
    
    from selenium import webdriver
    from selenium.webdriver.support.ui import Select
    from selenium.common.exceptions import NoSuchElementException
    from openpyxl import Workbook
    from openpyxl import load_workbook
    
    description = ["window"]
    ApplicationType = [0,11,12,31,32,33,46,47,61]
    #DecisionType
    startDate = "01/01/2012"
    endDate = "31/12/2012"
    error = False
    year = 2012
    
    #Excel
    wb = load_workbook('/Users/Caitlyn/Desktop/Program/Planning.xlsx')
    sheet = wb.get_sheet_by_name('Data')
    rowIndex = 2
    columnIndex = 1
    
    driver = webdriver.Chrome('/Users/Caitlyn/Downloads/chromedriver')
    driver.get(mainPage)
    
    # placing each description here
    sbox = driver.find_element_by_id("description")
    sheet.cell(row=rowIndex, column=columnIndex, value=description[0])
    columnIndex = columnIndex + 1
    sbox.send_keys(description[0])
    
    # here is where you will cycle through dates
    sbox = driver.find_element_by_id("applicationDecisionStart")
    sbox.send_keys(startDate)
    sbox = driver.find_element_by_id("applicationDecisionEnd")
    sbox.send_keys(endDate)
    sheet.cell(row=rowIndex, column=columnIndex, value=year)
    columnIndex = columnIndex + 1
    
    
        if error:
          resultsRow = rowIndex
          while True:
            column = driver.find_elements_by_class_name("searchresult")
            pageResults = pageResults + len(column)
            for element in column:
              list = element.text.split("\n")
              sheet.cell(row=rowIndex, column=columnIndex, value=list[0])
              columnIndex = columnIndex + 1
              sheet.cell(row=rowIndex, column=columnIndex, value=list[1])
              columnIndex = columnIndex + 1
              sheet.cell(row=rowIndex, column=columnIndex, value=list[2].split("|")[0])
              columnIndex = columnIndex - 2
              rowIndex = rowIndex + 1
            try:
              submit = driver.find_element_by_class_name("next")
              submit.click()
              pageIndex = pageIndex + 1
            except NoSuchElementException:
              break
    
          columnIndex = columnIndex - 1
          sheet.cell(row=resultsRow, column=columnIndex, value=pageResults)
          columnIndex = columnIndex - 1
        else:
          columnIndex = columnIndex - 1
          sheet.cell(row=rowIndex, column=columnIndex, value=0)
          columnIndex = columnIndex - 1
          rowIndex = rowIndex + 1
    
        driver.execute_script("window.history.go(-%d)" % pageIndex)
        error = False
    
      columnIndex = columnIndex - 1
    
    wb.save('/Users/Caitlyn/Desktop/Program/Planning.xlsx')
    

1 个答案:

答案 0 :(得分:0)

首先要做的是将代码分解为函数,以便将openpyxl代码与Selenium代码分开。

关于openpyxl代码:您应尽量避免使用ws.cell(…):您可以使用ws.append(…)将单元格列表附加到现有工作表。如果无法做到这一点,请使用enumerate()获取可靠的计数器。

我建议进一步的讨论应该转移到openpyxl邮件列表,在那里更容易提出部分问题。