Home > Back-end >  How to upload file using below HTML script in selenium python
How to upload file using below HTML script in selenium python

Time:10-24

I tried to send_keys() directly to attached path file but getting error can any on guide me how to upload file.

below is my selenium python script:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait



browser = webdriver.Chrome()
browser.maximize_window()
wait = WebDriverWait(browser,20)
browser.get('https://www.google.com/')

browser.find_element('xpath', '/html/body/div[1]/div[3]/form/div[1]/div[1]/div[1]/div/div[3]/div[3]/img').click()

att = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, '#ow6 > div.M8H8pb > c-wiz > div.ea0Lbe > div > div.gIYJUc > div.f6GA0 > div > div.ZeVBtc > span')))
att.send_keys('C:/Users/avu/caha.png')

CodePudding user response:

You are using a wrong web element.
To upload file with Selenium you need to use the following locator:

wait.until(EC.presence_of_element_located((By.XPATH, "//input[@type='file']"))).send_keys(file_path)

This element is not visible or clickable, so presence_of_element_located should be used in case you want to use WebDriverWait.
UPD
Your code should be as following:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait



browser = webdriver.Chrome()
browser.maximize_window()
wait = WebDriverWait(browser,20)
browser.get('https://www.google.com/')

browser.find_element(XPATH, "//img[@alt='Camera search']").click()

wait.until(EC.presence_of_element_located((By.XPATH, "//input[@type='file']"))).send_keys('C:/Users/avu/caha.png')

Possibly you even don't need to use browser.find_element(XPATH, "//img[@alt='Camera search']").click() line.

  • Related