1

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']"}

3 Answers 3

1

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")
Sign up to request clarification or add additional context in comments.

4 Comments

You need a Youtube channel: Professor Prophet Explains :)
Thank you @BarrythePlatipus, but I'm not a professor, just and engineer and even not of Computer Science but of Electronics :)
That's fine: 'Engineer Prophet Explains' then. You also have a working knowledge of coding applicability in real life, which eludes many a 'pure' coders nowadays.
Well, I'm working as an Automation engineer for last 7-8 years. And yes, while interviewing "senior" automation engineers I saw that at least 80% of them do not really understand many basic things they supposed to know.
0

Maybe you can try to use the CSS selector instead of XPATH?

Try this:

s = driver.find_element(By.CLASS_NAME, "css-yxxalb edgvbvh9")

1 Comment

I tried but didn't work. Here is the error: Message: no such element: Unable to locate element: {"method":"css selector","selector":".css-zl2jxg edgvbvh9"}
0

Increase the Implicitly Wait to at least 5 or 10 seconds.

driver.implicitly_wait(10)

It uploads the file correctly.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.