使用 Python Selenium Chromedriver 下载不一致 'Failed - Path too long' 错误

Inconsistent Download 'Failed - Path too long' Error using Python Selenium Chromedriver

堆栈:Windows 8.1, Python 3.4.3.4, Selenium 2.46, Chromedriver 2.16

我正在使用 Selenium Python 与 Chromedriver 的绑定来自动从以下 URL 下载一些内容:r'http://financials.morningstar.com/'

我已经设置了以下 chromedriver 首选项:

chromeOptions = webdriver.ChromeOptions()
prefs = {'download.default_directory':symbol_dir}
chromeOptions.add_experimental_option('prefs', prefs)
chromeOptions.add_extension(self.chromeUBlock_path)  
chromedriver_path = self.chromedriver_path

此外,我已经设置了以下 selenium 代码 运行 下载可以正常工作并下载到正确的文件位置等:

symbol = 'AAPL' # test symbol
IS_anl_statement = 'income-statement'
IS_anl_abbrev    = 'is'
url_anl = self.mstar_base_url + r'{}/{}.html?t={}&region=usa&culture=en-US'
IS_anl  = webdriver.Chrome(executable_path=chromedriver_path, chrome_options=chromeOptions)
IS_anl.set_page_load_timeout(90)

    try:        
        IS_anl.get(url_anl.format(IS_anl_statement, IS_anl_abbrev, symbol))
        anl_element = WebDriverWait(IS_anl, 90).until(EC.element_to_be_clickable(
            (By.LINK_TEXT, 'Annual')))
        anl_csv_element = WebDriverWait(IS_anl, 90).until(EC.element_to_be_clickable(
            (By.CSS_SELECTOR,self.css_path_export)))
        anl_csv_element.click()
        for i in range(1,10,1):
            time.sleep(i/50)
            if os.path.isfile(anl_file_string)==True:
                break    
    except Exception as e:
        print(e)

    IS_anl.quit()

但是,当 运行 使用以下缩写下载时(只需将 balance-sheet 替换为 income-statement,如下所示:

    BS_anl_statement = 'balance-sheet'
    BS_anl_abbrev    = 'bs'

和其余的 Selenium 代码完全相同我得到了可怕的下载错误: Failed-Path Too Long

这很奇怪,因为实际的文件路径并不太长。事实上,我在 3 个不同的目录中测试了下载,每个目录的文件路径都比上一个短。最终示例路径为:r"C:\mstar_data\"

我卡住了。我能看到的两次下载尝试之间的唯一区别是实际 CSV link。但即使在这种情况下,Income-Statement 下载 url 实际上比 Balance-Sheet 长,所以我又被卡住了。他们在这里:

I/S CSV url: http://financials.morningstar.com/ajax/ReportProcess4CSV.html?&t=XNAS:ZUMZ&region=usa&culture=en-US&cur=&reportType=is&period=12&dataType=A&order=asc&columnYear=5&curYearPart=1st5year&rounding=3&view=raw&r=573205&denominatorView=raw&number=3

B/S CSV url: http://financials.morningstar.com/ajax/ReportProcess4CSV.html?&t=XNYS:A&region=usa&culture=en-US&cur=&reportType=bs&period=12&dataType=A&order=asc&columnYear=5&curYearPart=1st5year&rounding=3&view=raw&r=558981&denominatorView=raw&number=3

对这种不一致的任何帮助都将是一个很大的帮助,绝对感激。谢谢。

我找到了解决此问题的方法。 @Skandigraun 是对的,有一段代码试图过早地使用下载的文件。

我用Advanced Uninstaller清除了所有临时文件、网络历史等等。 我重新启动了计算机。 我更改了以下关于 Skandigraun 评论的代码块:

for in range(1, 10, 1):
    time.sleep(i/50)
    if os.path.isfile(anl_file_string)==True:
            break 

至:

for i in range(1, 10, 1):
    time.sleep(i/20) # changed from 50 to 20
    if os.path.isfile(anl_file_string)==True:
            break 

我补充说:

finally:
    IS_anl.quit()

现在代码运行更加稳定。我相信 os 测试文件是否存在的调用也在发生 quickly/frequently 并且它导致了下载失败。我将 finally 语句添加到 try/except 循环中,以 close 浏览器,无论发生什么。如果访问站点出现问题、超时错误等,这可以防止脚本挂起。