python selenium循环链(循环内循环)迭代元素

时间:2018-01-16 14:54:48

标签: python selenium web-scraping datepicker

以下是我的代码。我试图通过迭代一个下拉框来下载zip文件,然后手动输入from和to日期。问题是,我无法输入from_date的第二个值,即我无法退出内部for循环。 任何帮助将不胜感激。

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time

browser=webdriver.Firefox()
date_from=['02-01-2000','02-01-2001','02-01-2002','02-01-2003','02-01-2004','02-01-2005','02-01-2006','02-01-2007','02-01-2008','02-01-2009','02-01-2010','02-01-2011','02-01-2012','02-01-2013','02-01-2014','02-01-2015','02-01-2016','02-01-2017'
]
date_to=['31-12-2000','31-12-2001','31-12-2002','31-12-2003','31-12-2004','31-12-2005','31-12-2006','31-12-2007','31-12-2008','31-12-2009','31-12-2010','31-12-2011','31-12-2012','31-12-2013','31-12-2014','31-12-2015','31-12-2016','31-12-2017']
browser.get('https://www.nseindia.com/products/content/equities/indices/historical_pepb.htm')

lists=['NIFTY 50',
'NIFTY Next 50',
'NIFTY Auto',
'NIFTY Bank',
'NIFTY Energy',
'NIFTY Financial Services',
'NIFTY FMCG',
'NIFTY IT',
'NIFTY Media',
'NIFTY Metal',
'NIFTY Pharma',
'NIFTY Private Bank',
'NIFTY PSU Bank',
'NIFTY Realty']
select = Select(browser.find_element_by_xpath('//*[@id="IndexName"]'))
browser.find_element_by_css_selector('#yield4').click()
for list in lists:
    select.select_by_visible_text(list)
    for date1 in date_from:
        browser.find_element_by_css_selector('#fromDate').send_keys(date1)
        for date2 in date_to:
            browser.find_element_by_css_selector('#toDate').send_keys(date2)
            browser.find_element_by_css_selector('#get').click()
            browser.execute_script("window.scrollTo(150, document.body.scrollHeight);")
            browser.find_element_by_css_selector('html body div#container div.content_big div#wrapper_btm div.main_content div.content div.archives div#replacetext.tabular-data-historic table tbody tr.alt td a.historical-csv-downld').click()
            browser.find_element_by_css_selector('#fromDate').clear()
            browser.find_element_by_css_selector('#toDate').clear() #after clearing the cell value, im not able to input the second value from the list


browser.quit()

1 个答案:

答案 0 :(得分:1)

我怀疑你的问题在于你正在清除#fromDate的内循环的倒数第二行。内部循环仅设置#toDate,因此如果清除#fromDate,则在内部循环完成之前不会设置它。相反,你应该只清除内循环中的#toDate,然后在内循环结束后,在outter循环中清除#fromDate。例如:

for date1 in fromDates:
    # set fromDate
    for date2 in toDates:
        # set toDate
        # do thing
        # clear toDate
    # clear fromDate

修改

根据您的评论,您希望使用第一个toDate等创建第一个fromDate来执行此操作,您可以遍历指示而不是每个列表中的元素:

assert(len(dates_from) == len(dates_to))
for i in range(len(dates_from)):
    date1 = dates_from[i]
    date2 = dates_to[i]
    # use dates 

代码中的结构如下所示:

for list in lists:
    select.select_by_visible_text(list)
    for i in range(len(date_from)):
        date1 = date_from[i]
        date2 = date_to[i]
        browser.find_element_by_css_selector('#fromDate').send_keys(date1)
        browser.find_element_by_css_selector('#toDate').send_keys(date2)
        browser.find_element_by_css_selector('#get').click()
        browser.execute_script("window.scrollTo(150, document.body.scrollHeight);")
        browser.find_element_by_css_selector('html body div#container div.content_big div#wrapper_btm div.main_content div.content div.archives div#replacetext.tabular-data-historic table tbody tr.alt td a.historical-csv-downld').click()
        browser.find_element_by_css_selector('#fromDate').clear()
        browser.find_element_by_css_selector('#toDate').clear()