Forums

selenium download csv file and read the file.

Hi, im new here, currently my code can run successful to login a website and download excel file, however the downloaded file place in my local directory, my question can we set a specific place for the downloaded file inside pythonanywhere? second how to read csv file in pythonanywhere?
cam pythonanywhere read the file directly in my local directory?

You are running your selenium code locally on your local machine? In that case, for your pythonanywhere code to access it, you would have to upload it to pythonanywhere.

Alternatively, you could run your selenium code on PythonAnywhere. Then you would be able to directly download into pythonanywhere and read from pythonanywhere.

There is a csv reader class in the standard python library.

my selenium code is automate to download excel file from a website every time i run the program, I running the code on PythonAnywhere for selenium code, the code run successful in PythonAnywhere, where I can find my downloaded file in PythonAnywhere?

I check on current working directory, but the file I download from the selenium code does not exist.

There is download.default_directory setting in your webdriver options.

[formatted by admin]

from pyvirtualdisplay import Display
from selenium import webdriver

with Display(): 
    print ('start')
#     driver = webdriver.Firefox()


    import os
    from selenium import webdriver

    profile = webdriver.FirefoxProfile()
    profile.set_preference('browser.download.folderList', 2)
    profile.set_preference('browser.download.manager.showWhenStarting', False)
    profile.set_preference('browser.download.dir', os.getcwd())
    profile.set_preference('browser.helperApps.neverAsk.saveToDisk', 'text/csv/xls')
    profile.set_preference("browser.helperApps.neverAsk.saveToDisk","text/csv,application/x-msexcel,application/excel,application/x-excel,application/vnd.ms-excel,image/png,image/jpeg,text/html,text/plain,application/msword,application/xml")

    profile.set_preference("browser.helperApps.neverAsk.openFile","text/csv,application/x-msexcel,application/excel,application/x-excel,application/vnd.ms-excel,image/png,image/jpeg,text/html,text/plain,application/msword,application/xml")

    driver = webdriver.Firefox(profile)

#     driver = webdriver.Chrome(executable_path=path,options=options)
    driver.get('website link')

    # driver = webdriver.ChromeOptions()

    username = driver.find_element_by_id("username")
    password = driver.find_element_by_id("password")
    username.send_keys('******'')
    password.send_keys('*******')
    time.sleep(2)
    driver.find_element_by_name("login").click()
    time.sleep(3)
    driver.find_element_by_xpath("/html/body/div[1]/div/div[2]/ul[1]/li[3]/a").click()
    #login
    print ('done')
    time.sleep(2)
    driver.find_element_by_xpath("/html/body/div[1]/div/div[2]/ul[1]/li[4]/a").click()
    time.sleep(3)
    print ('done')
    driver.find_element_by_xpath("/html/body/div[1]/div/div[2]/ul[1]/li[4]/ul/li[8]/a").click()


    time.sleep(2)
    driver.find_element_by_css_selector('#download_csv').click()
    time.sleep(120)
    driver.close()
    driver.quit()
    print ('done')

my code for the project, the code run without any error, but I cannot found the file. where I can find the downloaded file, which part of my code need to change or add for me to find the downloaded file in path "home/username"

Right now you have this line:

profile.set_preference('browser.download.dir', os.getcwd())

So if you want the files to go to /home/epireve you should change it to this:

profile.set_preference('browser.download.dir', "/home/epireve")

I have make changes on my code, but I still cannot found the downloaded file.... I really need help, I facing this problem since yesterday. tq

there is no error when running the code. but cannot find the downloaded file

It's possible that the site that you're accessing is not downloading the file at all; if you take a screenshot of the browser just after you've clicked on the download button, perhaps you'll see something useful? The code to do that would be this:

driver.get_screenshot_as_file("/home/epireve/download_screenshot.png")

That will put the file in your home directory.

I try run the code on local using Firefox, after click the download button there is pop up message asked about further action, how to handle the pop up message?

the pop up about to save the file or what something like that, how to handle the pop up message?

beside can pythonanywhere use chrome WebDriver to run selenium? if yes how to set the chrome path? or is there any example?

We'll continue this discussion in the email chain that we already have going.

can you help with this issue? I have try the code before, its work but after several of time I run the code, this error occur.

Traceback (most recent call last):
          File "pythonanywhere_billplz.py", line 35, in <module>
            driver = webdriver.Chrome(options=options)
          File "/home/epireve/.local/lib/python3.8/site-packages/selenium/webdriver/chrome/webdriver.py", line 76, in __init__
            RemoteWebDriver.__init__(
          File "/home/epireve/.local/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
            self.start_session(capabilities, browser_profile)
          File "/home/epireve/.local/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
            response = self.execute(Command.NEW_SESSION, parameters)
          File "/home/epireve/.local/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
            self.error_handler.check_response(response)
          File "/home/epireve/.local/lib/python3.8/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
            raise exception_class(message, screen, stacktrace)
        selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: crashed
          (unknown error: unable to discover open pages)
          (The process started from chrome location /usr/bin/chromium-browser is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
          (Driver info: chromedriver=2.42.591071 (0b695ff80972cc1a65a5cd643186d2ae582cd4ac),platform=Linux 5.4.0-1029-aws x86_64)

Do you run it headless?