首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Python系统通过系统窗口上传文件

使用Python系统通过系统窗口上传文件
EN

Stack Overflow用户
提问于 2019-04-09 15:05:03
回答 1查看 1.2K关注 0票数 0

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

但,

亲爱所有的Python爱好者们,为什么不能使用呢?所以这个任务。

在JAVA:http://www.zamzar.com/解决方案(& JAVA代码)中在堆栈溢出:如何使用Selenium WebDriver处理windows文件上传?中解决

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

任务(上传文件)我正在做的很简单:网站:https://www.wordtopdf.com/

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

Note_2:下载和解压缩铬驱动程序后,您将不得不在下面的代码中艰难地输入2条路径(注释中的链接)。这两个路径是:(/any) word文件的路径&解压缩的铬驱动程序的b路径。

我的守则:

代码语言:javascript
复制
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文件上传?)中类似的东西,这应该是一种魅力。但是沃拉..。完全失败,从而有机会学到新的东西。

我也尝试过:

代码语言:javascript
复制
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:与系统模式对话框交互),“警报”应该是内置的功能。

但是上面链接中的'Alert‘函数在我的Python安装程序中似乎不存在,即使在执行之后

代码语言:javascript
复制
from selenium import webdriver

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

干杯

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-04-09 18:54:34

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

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

代码语言:javascript
复制
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 /LinuxMint19.1/Python3.7/ Selenium 3.141.0进行测试

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

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

代码语言:javascript
复制
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()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55595886

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档