亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

通過系統窗口使用 Python Selenium System 上傳文件

通過系統窗口使用 Python Selenium System 上傳文件

Go
素胚勾勒不出你 2022-01-05 10:12:17
我正在使用一個試用網站案例來學習使用 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:我不需要這個工具來做任何工作,因為有更好的包來做這個 word 到 pdf 的轉換。相反,這僅用于學習和完善 Python Selenium 代碼/應用程序。Note_2:下載并解壓 chrome 驅動程序后,您將不得不在下面的代碼中煞費苦心地輸入 2 個路徑(下面的評論中的鏈接)。這 2 個路徑是:[a] a(/any) word 文件的路徑和 [b] 解壓縮的 chrome 驅動程序的路徑。我的代碼:from selenium import webdriverUNZIPPED_DRIVER_PATH = 'C:/Users/....' # You need to specify this on your computerdriver = 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 peaceFind_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 computerFind_upload.send_keys(WORD_FILE_PATH) # Not working, no action happens here基于 JAVA 中非常相似的東西(How to handle windows file upload using Selenium WebDriver?),這應該很有魅力。但是瞧……完全失敗,因此有機會學習新東西。我也試過: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@所有讀者,希望這不會占用您太多時間,我們都可以從中學到一些東西。
查看完整描述

1 回答

?
MMMHUHU

TA貢獻1834條經驗 獲得超8個贊

你得到('//*[@id="file-uploader"]')哪個是<a>標簽


但是你必須使用隱藏的<input type="file">(后面<a>)


import selenium.webdriver


your_file = "/home/you/file.doc"

your_email = "[email protected]"


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()


查看完整回答
反對 回復 2022-01-05
  • 1 回答
  • 0 關注
  • 289 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號