首页 > 解决方案 > 本地工作的硒浏览器下载文件自动化代码在gcp linux服务器中不起作用

问题描述

我正在尝试从客户端仪表板自动下载和处理某个 xlsx 和 csv 文件。下面的代码适用于我的本地,在无头和有头模式下。我还可以在本地手动下载文件。但是,当我在我的 gcp linux 服务器上运行代码时,我无法下载该文件。该文件很大,需要定期导入,因此我需要将其放入服务器中的工作 cron 中。任何人都可以查看这段代码并告诉我应该怎么做才能将文件下载到我的服务器中吗?

谢谢

我的代码:

def bil():
    options = webdriver.ChromeOptions()
    options.add_argument('--headless')
    options.add_argument("disable-infobars")
    options.add_argument("--disable-extensions")
    options.add_argument("--disable-notifications")
    Download_dir = ('/my/path')
    preferences = {"download.default_directory": Download_dir ,
                       "download.prompt_for_download": False,
                       "directory_upgrade": True,
                       "safebrowsing.enabled": True }
    options.add_experimental_option("prefs", preferences)
    path = '/Users/chromedriver'
    driver = webdriver.Chrome(executable_path=path,options=options)
    driver.get('https://dashboard.bill.com')
    username = driver.find_element_by_id("user_email")
    password = driver.find_element_by_id("user_password")
    username.send_keys('try@gmail.com')
    password.send_keys('123456')
    driver.find_element_by_xpath('//*[@id="new_user"]/input[3]').click()
    time.sleep(3)
    driver.find_element_by_xpath('/html/body/div[1]/div/div[1]/a[1]').click()
    datepicker = driver.find_element_by_xpath('//*[@id="account_payment_search"]/div[1]/div[2]/div/div[1]/div/div/div/div/input')
    today = date.today()
    from_date = today - timedelta(1)
    from_date = from_date.strftime('%-d %b %Y')    
    driver.execute_script('$("input.form-control.daterange").data("daterangepicker").setStartDate(arguments[0])',from_date)
    driver.execute_script('$("input.form-control.daterange").data("daterangepicker").setEndDate(arguments[0])',from_date)    
    driver.find_element_by_css_selector('input.form-control.daterange').click()
    driver.find_element_by_css_selector('div.range_inputs > button.applyBtn').click()
    time.sleep(3)    
    driver.find_element_by_xpath('//*[@id="account_payment_search"]/div[2]/div[2]/div[1]/div/ul/li/button/span[2]').click()    
    time.sleep(5)
    driver.refresh()
    while driver.find_element_by_css_selector('#report_downloads > tr:nth-child(1) > td:nth-child(4)').text != 'completed':
        time.sleep(10)
        driver.refresh()
    download_data = driver.find_element_by_css_selector('#report_downloads > tr:nth-child(1) > td:nth-child(5) > a')
    download_data.click()
    time.sleep(10)
    driver.close()
    driver.quit() 
    sleep(10)
    arr = os.listdir(Download_dir)
    for i in arr:
        if 'xlsx' in i:
            df_billplz_my =pd.read_excel(Download_dir+f'/{i}') 
    return df_billplz_my

myfa = bil()

在我的服务器上运行时,我得到以下输出:

UnboundLocalError: local variable 'df_billplz_my' referenced before assignment

蟒蛇版本:Python 3.8

我在文件下载的最后收到消息-但不要混淆,它没有下载,这只是我保留的一条消息,以显示完全执行的代码。无论我添加多少超时,下载目录都保持为空。PS:我不确定这是否重要,但我已经掩盖了 url/client 的名称。

可能是什么问题 ?

标签: python-3.xlinuxgoogle-cloud-platform

解决方案


推荐阅读