如何使用Python和Selenium将多个文件上传到网站?

问题描述 投票:0回答:1

尝试使用 Python 和 Selenium 将多个文件上传到网站,但只选择第一个文件,而不选择其他文件,知道为什么吗?它们实际上是 PGP 文件,但我认为这并不重要

不确定我的脚本出了什么问题,我使用 Selenium 和 Glob 作为上传和文件操作的主要来源。我已将所有文件设置为变量,并且还准确地使用了 send.keys,不知道为什么它只获取每个文件的第一个实例而不是所有文件。

例如,所有这些都指向的目录文件夹也可能有 2 或 3 个“ack”文件。当我执行此代码时,它只会选取第一个,而错过其他 2 个。

    # this imports all the packages that we need to run our script
import glob
import datetime
import selenium
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time


# this creates the format for the name convention of today
today = datetime.datetime.today().strftime("%Y%m%d")


# these are the files that will be uploaded to Chase
print("The following files have been found and uploaded to Chase:")

b3input = glob.glob(r'S:\Chase\up\b3pinput_MOORCL_' + today + '*.pgp')
pmntr = glob.glob(r'S:\Chase\up\b3pmntr_MOORCL_' + today + '*.pgp')
ack = glob.glob(r'S:\Chase\up\ack_b3ptran_LIT_MOORCL_' + today + '*.pgp')

# the following file variable is for redundancy (to pick up any file that is a PGP File)
for pgpFiles in glob.glob(r'S:\Chase\up\*.pgp'):
    print(pgpFiles)



# this gives the location of our chrome driver
driver_path = r'C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\chromedriver.exe'
driver = webdriver.Chrome(executable_path=driver_path)



# this tells the driver to go to the following website
driver.get('https://website.com')

delay = 10 # seconds

try:
    # Waits for 10 seconds looking for the user name text area
    login_box = WebDriverWait(driver, delay).until(
        EC.presence_of_element_located((By.ID, "userIDInput"))
    )
    login_box.send_keys('username')

    # Waits for 10 seconds looking for the password text area
    password_box = WebDriverWait(driver, delay).until(
        EC.presence_of_element_located((By.ID, "passwordInput"))
    )
    password_box.send_keys('password')

    login_button = driver.find_element_by_id('loginButon')
    login_button.click()

    chase_ToJPM = driver.get('https://website.com/To_JPMC')

    #upload_files = driver.find_element_by_id('allFiles_actions::upload')
    #upload_files.click()

    upload_files = WebDriverWait(driver, 30).until(
        EC.presence_of_element_located((By.ID, "allFiles_actions::upload"))
    )

    #upload_files.send_keys(b3input)
    #upload_files.send_keys(pmntr)
    #upload_files.send_keys(ack)
    upload_files.send_keys(pgpFiles)

    time.sleep(7)

    driver.quit()

  
except TimeoutError:
    print("Loading took too much time!")
python selenium file selenium-webdriver upload
1个回答
4
投票

您可以使用以下代码行上传多个文件:

uploadElement.send_keys("S:\Chase\up\abc.pgp \n S:\Chase\up\pqr.pgp \n S:\Chase\up\xyz.pgp")

参考文献

您可以在以下位置找到一些相关的详细文档:

© www.soinside.com 2019 - 2024. All rights reserved.