首页 > 解决方案 > 通过系统窗口使用 Python Selenium System 上传文件

问题描述

我正在使用一个试用网站案例来学习使用 Python Selenium 上传文件,其中上传窗口不是 HTML 的一部分。上传窗口是系统级更新。这已经使用 JAVA(下面的 stackoverflow 链接)解决了。如果通过 Python 无法做到这一点,那么我打算转向 JAVA 来完成这项任务。

但,

亲爱的所有 Python 爱好者,为什么不能使用 Python webdriver-Selenium。于是有了这个追求。

在 JAVA 中解决 URL:http ://www.zamzar.com/ stackoverflow 中的解决方案(和 JAVA 代码):如何使用 Selenium WebDriver 处理 Windows 文件上传?

这是我的 Python 代码,应该是不言自明的,包括 chrome webdriver 下载链接。

任务(上传文件)我正在简要尝试:网站:https ://www.wordtopdf.com/

Note_1:我不需要这个工具来做任何工作,因为有更好的包来做这个词到 pdf 的转换。相反,这只是为了学习和完善 Python Selenium 代码/应用程序。

注意_2:下载并解压缩 chrome 驱动程序后,您将不得不在下面的代码中煞费苦心地输入 2 条路径(下面的评论中的链接)。这 2 个路径是:[a] a(/any) word 文件的路径和 [b] 解压缩的 chrome 驱动程序的路径。

我的代码:


from selenium import webdriver
UNZIPPED_DRIVER_PATH = 'C:/Users/....' # You need to specify this on your computer

driver = webdriver.Chrome(executable_path = UNZIPPED_DRIVER_PATH)

# Driver download links below (check which version of chrome you are using if you don't know it beforehand):
# Chrome Driver 74 Download: https://chromedriver.storage.googleapis.com/index.html?path=74.0.3729.6/
# Chrome Driver 73 Download: https://chromedriver.storage.googleapis.com/index.html?path=73.0.3683.68/
New_Trial_URL = 'https://www.wordtopdf.com/'

driver.get(New_Trial_URL)
time.sleep(np.random.uniform(4.5, 5.5, size = 1)) # Time to load the page in peace

Find_upload = driver.find_element_by_xpath('//*[@id="file-uploader"]')

WORD_FILE_PATH = 'C:/Users/..../some_word_file.docx' # You need to specify this on your computer

Find_upload.send_keys(WORD_FILE_PATH) # Not working, no action happens here

基于 JAVA 中非常相似的东西(如何使用 Selenium WebDriver 处理 windows 文件上传?),这应该像一个魅力。但是瞧……完全失败,因此有机会学习新事物。

我也试过:

Click_Alert = Find_upload.click()
Click_Alert(driver).send_keys(WORD_FILE_PATH)

不工作。“警报”应该是根据这两个链接的内置功能(https://seleniumhq.github.io/selenium/docs/api/py/webdriver/selenium.webdriver.common.alert.html & Selenium-Python:与系统交互模态对话框)。

但是即使在执行之后,我的 Python 设置中似乎也不存在上述链接中的“警报”功能

from selenium import webdriver

@所有读者,希望这不会占用您太多时间,我们都可以从中学到一些东西。

干杯

标签: pythonselenium-chromedriver

解决方案


你得到('//*[@id="file-uploader"]')哪个是<a>标签

但是您必须使用隐藏的<input type="file">(后面)<a>

import selenium.webdriver

your_file = "/home/you/file.doc"
your_email = "you@example.com"

url = 'https://www.wordtopdf.com/'

driver = selenium.webdriver.Firefox()
driver.get(url)

file_input = driver.find_element_by_xpath('//input[@type="file"]')
file_input.send_keys(your_file)

email_input = driver.find_element_by_xpath('//input[@name="email"]')
email_input.send_keys(your_email)

driver.find_element_by_id('convert_now').click()

使用 Firefox 66 / Linux Mint 19.1 / Python 3.7 / Selenium 3.141.0 测试


编辑:在zamzar.com上上传的方法相同

我第一次看到的情况(所以我花了更长的时间来创建解决方案):它<input type="file">隐藏在按钮下,但它不使用它来上传文件。它动态创建第二个<input type="file">用于上传文件(甚至可能是许多文件 - 我没有测试它)。

import selenium.webdriver
from selenium.webdriver.support.ui import Select
import time


your_file = "/home/furas/Obrazy/37884728_1975437959135477_1313839270464585728_n.jpg"
#your_file = "/home/you/file.jpg"
output_format = 'png'

url = 'https://www.zamzar.com/'
driver = selenium.webdriver.Firefox()
driver.get(url)

#--- file --- 

# it has to wait because paga has to create second `input[@type="file"]`
file_input = driver.find_elements_by_xpath('//input[@type="file"]')
while len(file_input) < 2:
    print('len(file_input):', len(file_input)) 
    time.sleep(0.5)
    file_input = driver.find_elements_by_xpath('//input[@type="file"]')

file_input[1].send_keys(your_file)

#--- format ---

select_input = driver.find_element_by_id('convert-format')      
select = Select(select_input)
select.select_by_visible_text(output_format)

#--- convert ---

driver.find_element_by_id('convert-button').click()

#--- download ---

time.sleep(5)

driver.find_elements_by_xpath('//td[@class="status last"]/a')[0].click()

推荐阅读