Search code examples
python-3.xseleniumselenium-chromedriver

.sendkeys method not working to upload file using Python Selenium


I'm trying to automate facebook marketplace posts. But i'm struggling to upload pictures to it.

I already locate the element. When i click the element it will show the 'box' showing the file manager so that i can click on the folders and then the desired image.

ele = wait.until(EC.element_to_be_clickable((By.XPATH,'//*[@id="rc.js_c"]/div/div[1]/div[5]/div[2]/div/div/div/div/div[1]/div/div/span/div/a/div[2]')))
ele.click()

But when i try this:

ele.send_keys('/file_path/rasp.jpeg')

It raises this exception:

selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable

I also tried using the os library:

ele.send_keys(os.getcwd() + '/home/br1/Downloads/rasp.jpeg')

Getting the same exception error.

The html code where the element is visible (element used in code):

<div class="_3jk">

which is the parent of (where the element is not visible):

<input accept="image/*" multiple="" name="composer_photo" title="Elige un archivo para subir" data-testid="add-more-photos" display="inline-block" type="file" class="_n _5f0v" id="js_wg">

Here is all the code if you want to try it:

 from selenium import webdriver
 from selenium.webdriver.chrome.options import Options
 from selenium.webdriver.support import expected_conditions as EC
 from selenium.webdriver.support.ui import WebDriverWait
 from selenium.webdriver.common.by import By 10 
 # driver protocols
 options = Options()
 options.add_argument('disable-notifications')
 options.add_argument('start-maximized')
 driver = webdriver.Chrome(options=options, executable_path='/chromedriver')
 wait = WebDriverWait(driver,10)
 # url
 driver.get('http://facebook.com/marketplace')
 driver.implicitly_wait(10)
 # logging
 driver.find_element_by_id('email').send_keys('username')
 driver.find_element_by_id('pass').send_keys('password')
 driver.find_element_by_id('u_0_2').click()
 # entering marketplace
 driver.find_element_by_xpath('//*[contains(text(), "Vender algo")]').click()
 driver.find_element_by_xpath('//*[contains(text(), "Artículo en venta")]').click()
 ele = wait.until(EC.element_to_be_clickable((By.XPATH,'//*[@id="rc.js_c"]/div/div[1]/div[5]/div[2]/div/div/div/div/div[1]/div/div/span/div/a/div[2]')))
 ele.send_keys('/file_path/rasp.jpeg')

Any ideas and suggestions will be aprecciate it. I'm a Linux user.


Solution

  • You should try using the input to send the file path rather the div.

    Try the below.

    ele = wait.until(EC.presence_of_element_located((By.XPATH,'//input[@name="composer_photo" and @type="file"]')))
    ele.send_keys("file_to_be_uploaded")