I'm trying to upload a CSV file to this website: https://maalaei97-test3.hf.space/?__theme=light using selenium. I tried to find elements by XPath and Partial_link_text but none of them worked.
Here is the code I used:
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")
And this is the error I receive:
Message: no such element: Unable to locate element: {"method":"xpath","selector":"//input[@type='file']"}
CodePudding user response:
You need to wait for page to be loaded before trying uploading the file.
The best approach to do so is to use WebDriverWait
expected_conditions
explicit waits.
The following code worked properly for me:
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")
CodePudding user response:
Maybe you can try to use the CSS selector instead of XPATH?
Try this:
s = driver.find_element(By.CLASS_NAME, "css-yxxalb edgvbvh9")
CodePudding user response:
Increase the Implicitly Wait to at least 5 or 10 seconds.
driver.implicitly_wait(10)
It uploads the file correctly.