我使用Python 2.7中的Selenium 3.4.3来填写一个web表单并下载生成的CSV文件。使用Chromedriver,脚本会一直执行,但下载文件夹中的文件看起来像这样10494_20170829000000.csv.crdownload,它是0kb。在我关闭Chrome和Python外壳之后,这种情况不会改变。我尝试过更改默认的下载目录,但它总是转到C:/downloads,并且总是有crdownload扩展名。
driver = webdriver.Chrome(executable_path='C:Python27ArcGISx6410.3Scriptschromedriver.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
我想明白了。浏览器在文件下载完成之前就关闭了,所以我在driver.quit((.之前添加了time.sleep(10(