Subham
Subham

Reputation: 701

File does not being downloaded in Python using Selenium | Chromium

I have python script that downloads file from website. When I pass parameter from the terminal, then Python script runs in background and downloads file successfully. But when I pass parameter from PHP file then script runs but does not download the file.

I am using Selenium and Chrome-webdriver in headless mode.

Can anyone help me to find out where I am doing wrong? I am running script on Ubuntu Linux OS..

My PHP Script

 <form method="post" action="CsvDownload.php" >
        <label>Store Id</label>
        <input type="text" class="form-control" name="storeId" id="store_id">
    <button type="submit" class="btn btn-success"> Submit</button>
 </form>

<?php

    ini_set('max_execution_time', 300);
    $store_id = false;
    if(isset($_POST['storeId'])){
        $store_id = $_POST['storeId'];
     } 
    $command = "python python_file.py -s $store_id";
    $output = exec($command);

    echo $output;

  ?>

My Python Script

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.keys import Keys
import time
import csv
import os
import glob
import random
from datetime import date
import psutil
import requests
import json
import sys

count=0
for arg in sys.argv:
    if arg == "-s":
        temp = sys.argv[count+1]
    count+=1
store_id=int(temp)
def downloadCSV(store_id,user_name,user_password):
    options = Options()
    PROCNAME = "chromedriver" # to clean up zombie Chrome browser

    download_url = "/var/www/mywebsite.com/public_html/python_files/"
    options.add_experimental_option("prefs", {
      "download.default_directory": download_url,
      "download.prompt_for_download": False,
      "download.directory_upgrade": True,
      "safebrowsing.enabled": True
    })

    options.add_argument("download.default_directory = /var/www/mywebsite.com/public_html/python_files")
    options.add_argument('--headless')
    options.add_argument('--no-sandbox')
    driver = webdriver.Chrome(chrome_options=options, executable_path=r'/usr/local/bin/chromedriver')
    driver.command_executor._commands["send_command"] = ("POST", '/session/$sessionId/chromium/send_command')
    params = {'cmd': 'Page.setDownloadBehavior', 'params': {'behavior': 'allow', 'downloadPath': download_url}}
    command_result = driver.execute("send_command", params)
    #open amazon url

    driver.get("https://www.amazonlogistics.in/comp/packageSearch")


    driver.find_element_by_id('ap_email').send_keys(user_name)
    driver.find_element_by_id ('ap_password').send_keys(user_password)

    driver.find_element_by_id('signInSubmit').click()
    time.sleep(4)


    driver.get("https://www.amazonlogistics.in/comp/packageSearch")
    time.sleep(3)


    #select update status date
    driver.find_elements_by_css_selector("input[type='radio'][value='deliveryDate']")[0].click()
    time.sleep(3)
    Select(driver.find_element_by_xpath("//select[@id='shipStatusIdList']"))


    dropdown_options = select_box.options

    select_box = Select(driver.find_element_by_xpath("//select[@id='shipStatusIdList']"))
    x=[select_box.select_by_index(ele_index) for ele_index in range(len(dropdown_options)) ]
    time.sleep(3)




    driver.find_element_by_id('downloadAsCSV').click()
    time.sleep(15)


    driver.find_element_by_class_name('log-out').click()
    time.sleep(3)
    driver.quit()
    y=[proc.kill() for proc in psutil.process_iter() if proc.name() == PROCNAME]
     # check whether the process name matches
    #get latest csv file
    list_of_files = glob.glob(download_url+'/*')

    latest_csv = max(list_of_files, key=os.path.getctime)
    today = date.today()
    new_file_name = "package_search_{}_10pm_{}_{}_{}.csv".format(store_id,today.day,today.month,today.year)
    print(new_file_name)
    os.rename(latest_csv,os.path.join(download_url,new_file_name))
    data = {
      "file_name":new_file_name,
      "message":"file downloaded successfully",
      "status_code": 200
    }
    print(json.dumps(data))



list_of_data=[(1,"username","password"),(3,"username2","password2").......]

if store_id:
    for data in list_of_data:
        if data[0]==store_id:
            downloadCSV(data[0],data[1],data[2])
        else:
            print("not suceess")
else:
    print("end")

I don't get any error but file doesn't download while running from PHP

Upvotes: 0

Views: 285

Answers (1)

Benjie Perez
Benjie Perez

Reputation: 76

Can you check your download URL write permission? If not change it to 755.

Also try to this instead of: $command = "python python_file.py -s $store_id"; Make it: $command = "python python_file.py -s ".$store_id;

Upvotes: 1

Related Questions