我想自動將檔案上傳到這個網站(這不是我的網站),但是這個網站是在一些 js 框架中創建的(我認為它是反應)。現在我上傳檔案時遇到問題,我嘗試過的一切都不起作用。我使用 Linux(發行版 Manjaro),但無法使用 AutoIT。
這是我嘗試過的。
file_image = 'image.jpg'
#this
uplod_image = browser.find_element_by_xpath('//input[@qa-id="selectFile"]').send_keys(file_image)
#and this
upload_image = browser.find_element_by_class_name("fileUploadBtn_dropzoneElement_38Gmm").send_keys(file_image)
這是經過檢查的代碼,主要問題是上傳是像div

我經常遇到這個錯誤...
selenium.common.exceptions.ElementNotInteractableException: Message: Element <div class="fileUploadBtn_dropzoneElement_38Gmm"> is not reachable by keyboard
Stacktrace:
WebDriverError@chrome://remote/content/shared/webdriver/Errors.jsm:183:5
ElementNotInteractableError@chrome://remote/content/shared/webdriver/Errors.jsm:293:5
webdriverSendKeysToElement@chrome://remote/content/marionette/interaction.js:624:13
interaction.sendKeysToElement@chrome://remote/content/marionette/interaction.js:600:11
sendKeysToElement@chrome://remote/content/marionette/actors/MarionetteCommandsChild.jsm:497:24
receiveMessage@chrome://remote/content/marionette/actors/MarionetteCommandsChild.jsm:151:31
知道如何解決這個問題嗎?
uj5u.com熱心網友回復:
如果您檢查input元素,則樣式屬性顯示為display: none;
這就是即使使用有效定位器也無法互動的原因。
您需要將元素的樣式更改為display: block;,然后嘗試使用上傳檔案send_keys()
使用 java 腳本執行器更改樣式。
fileupload=WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, "div[qa-id='dropZone']>input[type='file']")))
driver.execute_script("arguments[0].style.display = 'block';",fileupload)
fileupload.send_keys(path/to/file)
希望這段代碼對你有用。
您需要匯入以下庫。
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
uj5u.com熱心網友回復:
通常使用 Selenium 上傳檔案是通過以下方式完成的:
browser.find_element_by_xpath('//input[@type="file"]').send_keys(file_location)
file_location本地 PC 上檔案的實際絕對路徑在哪里。
喜歡C:/path_to_file/image.jpg
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/433118.html
