简体   繁体   中英

Not able to uplaod file in selenium python

I'm not able to upload file from my local. Below is the code I'm using to upload file:

BrowseElement = driver.find_element(By.CSS_SELECTOR, "span[class='image-icon material-icons-round post_image-icon__37fM5']")
BrowseElement.send_keys("C://Users//deepa//Downloads//deepak_1.png")

Below is the error I'm getting:

raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable

Please help me out.

Thanks in advance.

import os
from selenium import webdriver

driver = webdriver.Chrome()
driver.get("http://www.google.com")
driver.find_element_by_name("q").send_keys("selenium python")
driver.find_element_by_name("btnK").click()

# get the current directory
current_dir = os.getcwd()

# get the file path
file_path = os.path.join(current_dir, "test.txt")

# upload the file
driver.driver.find_element_by_css_selector("file-upload").send_keys(file_path)

# close the browser
driver.close()

I generated it using https://askjarvis.io/protected/demo.html , you can also do it if it doesn't exactly answer what you are trying to do.

If your web page has at least one input field with type as file, then the below code should work

BrowseElement = driver.find_element(By.CSS_SELECTOR, "input[type='file']")
BrowseElement.send_keys("C://Users//deepa//Downloads//deepak_1.png")

Please check in the dev tools (Google chrome) if we have unique entry in HTML DOM or not.

Css that you should check:

input[type='file']

Steps to check:

Press F12 in Chrome -> go to element section -> do a CTRL + F -> then paste the css and see, if your desired element is getting **highlighted** with 1/1` matching node.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM