Python Selenium Chromedriver文件下载不起作用

时间:2017-08-31 21:43:45

标签: python-2.7 csv selenium selenium-chromedriver

我在Python 2.7中使用Selenium 3.4.3来填写Web表单并下载生成的CSV文件。使用Chromedriver,脚本会一直运行,但下载文件夹中的文件看起来像 10494_20170829000000.csv.crdownload ,它是0kb。关闭Chrome和Python shell后,这不会改变。我已经尝试更改默认下载目录,但它总是转到C:/ downloads并且始终具有crdownload扩展名。

driver = webdriver.Chrome(executable_path='C:\Python27\ArcGISx6410.3\Scripts\chromedriver.exe')
driver.get(url)


def find_by_xpath(locator):
element = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.XPATH, locator))
)

return element

class FormPage(object):

def fill_form(self, data):

    find_by_xpath('//input[@name = "h_UserName"]').send_keys(data['h_UserName'])
    find_by_xpath('//input[@name = "h_Password"]').send_keys(data['h_Password'])
    find_by_xpath('//input[@name = "h_go"]').click()
    find_by_xpath('//select[@name = "dldataformat"]').send_keys(data['dldataformat'])
    find_by_xpath('//select[@name = "startyear"]').send_keys(data['startyear'])
    find_by_xpath('//select[@name = "startmonth"]').send_keys(data['startmonth'])
    find_by_xpath('//select[@name = "startday"]').send_keys(data['startday'])
    find_by_xpath('//input[@name = "duration"]').click()
    find_by_xpath('//select[@name = "endyear"]').send_keys(data['endyear'])
    find_by_xpath('//select[@name = "endmonth"]').send_keys(data['endmonth'])
    find_by_xpath('//select[@name = "endday"]').send_keys(data['endday'])


    return self # makes it so you can call .submit() after calling this function

def submit(self):
    find_by_xpath('//input[@name = "cmd"]').click()

data = {
'h_UserName':'',
'h_Password':'',
'dldataformat': '0',
'startyear': '2017',
'startmonth': '8',
'startday': '29',
'endyear': '2017',
'endmonth': '8',
'endday': '30'
}

FormPage().fill_form(data).submit()
driver.quit() # closes the webbrowser

1 个答案:

答案 0 :(得分:2)

我明白了。浏览器在文件下载完成之前关闭,所以我在driver.quit()之前添加了time.sleep(10)。