如何在這行代碼中上傳影像。
細節,我需要點擊我要上傳檔案的欄位,我不能使用命令 SEND_KEYS(file path)
這是我正在使用的代碼。
我發現的另一種選擇是 pyautogui,但我不喜歡它,因為它使用鍵盤來執行命令。
foto = driver.find_element(By.XPATH, "/html[1]/body[1]/div[1]/div[1]/div[1]/div[1]/div[4]/div[1]/div[1]/div[1]/div[1]/div[2]/div[1]/div[1]/div[1]/div[1]/div[2]/div[1]/div[4]/div[1]/div[2]/div[1]/div[1]/div[1]/div[1]/div[1]/div[1]/div[1]/i[1]").click()
sleep(5)
pyautogui.write(r"C:\__Imagens e Planilhas Python\Facebook\Imagens\nome3.png")
pyautogui.press("enter")
打開以上傳檔案的螢屏
在此處輸入影像描述
uj5u.com熱心網友回復:
使用 Selenium 上傳檔案通常使用以下代碼完成:
uploading_element = driver.find_element(By.XPATH, "//input[@type='file']")
uploading_element.send_keys(path_to_the_file_to_be_uploaded)
在你的情況下,這應該作業:
uploading_element = driver.find_element(By.XPATH, "//input[@type='file']")
uploading_element.send_keys("C:\__Imagens e Planilhas Python\Facebook\Imagens\nome3.png")
無需發送Keys.ENTER,無需使用r路徑。
確保在執行此操作時頁面已正確加載。
可能您需要添加一個等待,因此您的代碼可以如下所示:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 20)
wait.until(EC.presence_of_element_located((By.XPATH, "//input[@type='file']"))).send_keys("C:\__Imagens e Planilhas Python\Facebook\Imagens\nome3.png")
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/515480.html
下一篇:僅在單個div中選擇單選按鈕
