dongqian5639 2019-05-27 17:08
浏览 172

使用Selenium |不会在Python中下载文件 铬

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

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 求解 yolo算法问题
    • ¥15 虚拟机打包apk出现错误
    • ¥30 最小化遗憾贪心算法上界
    • ¥15 用visual studi code完成html页面
    • ¥15 聚类分析或者python进行数据分析
    • ¥15 三菱伺服电机按启动按钮有使能但不动作
    • ¥15 js,页面2返回页面1时定位进入的设备
    • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
    • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
    • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝