我正在嘗試將 CSV 檔案上傳到此網站:https ://maalaei97-test3.hf.space/?__theme=light using selenium。我試圖通過 XPath 和 Partial_link_text 查找元素,但它們都不起作用。
這是我使用的代碼:
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome(executable_path="C:\chromedriver.exe")
driver.implicitly_wait(0.5)
driver.maximize_window()
driver.get("https://maalaei97-test3.hf.space/?__theme=light")
#to identify element
s = driver.find_element(By.XPATH, "//input[@type='file']")
#file path specified with send_keys
s.send_keys("F:/elmo sanat/thesis/design/pythonProject/df.csv")
這是我收到的錯誤:
Message: no such element: Unable to locate element: {"method":"xpath","selector":"//input[@type='file']"}
uj5u.com熱心網友回復:
在嘗試上傳檔案之前,您需要等待頁面加載。
最好的方法是使用WebDriverWait expected_conditions顯式等待。
以下代碼對我來說正常作業:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
options = Options()
options.add_argument("start-maximized")
webdriver_service = Service('C:\webdrivers\chromedriver.exe')
driver = webdriver.Chrome(options=options, service=webdriver_service)
wait = WebDriverWait(driver, 20)
url = "https://maalaei97-test3.hf.space/?__theme=light"
driver.get(url)
wait.until(EC.presence_of_element_located((By.XPATH, "//input[@type='file']"))).send_keys("C:/Users/my_user/Downloads/my_file")
uj5u.com熱心網友回復:
也許您可以嘗試使用 CSS 選擇器而不是 XPATH?
嘗試這個:
s = driver.find_element(By.CLASS_NAME, "css-yxxalb edgvbvh9")
uj5u.com熱心網友回復:
將隱式等待時間增加到至少 5 或 10 秒。
driver.implicitly_wait(10)
它正確上傳檔案。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/532179.html
下一篇:在執行緒“main”java.lang.NoClassDefFoundError中出現錯誤例外:無法初始化類org.codehaus.groovy.reflection.ReflectionCache
