1

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')

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

10 Comments

then also file not got attached. I m using path like this file_path = "C:\\Users\\automation_new\\cvu\\12.zip" is this ok
Try using single, not double. Also, sometimes there are several elements matching "//input[@type='file']" locator. In this case you will need to target the proper element precisely
When i use single \ code editor throws error
Well, your question is missing debugging details..
can u tell me how to attach zip file using above HTML script
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.