Rookie programmer
Rookie programmer

Reputation: 25

Selenium Python - File upload over Windows

Final task is to upload photos to site.

When i click on Upload photo - Window from Windows pops up and ask user to select pictures

I managed somehow to click on Upload and the window opens.

try:
    myElem = WebDriverWait(driver, delay).until(EC.presence_of_element_located((By.XPATH, '/html/body/div[1]/main/div[2]/div/div/div[2]/section[1]/div/div[2]/div/label/div/div[1]/div/div'))) #.click(    
    myElem.click()
    myElem.SendKeys("path")
except TimeoutException:
    errorDuringFill = True

Also tried with pyWinAuto and pyAutoIt

Wasn't able to accomplish it.

Using:

webdriver.Firefox() 

Edit HTML:

<div class="listing-editor__box-content">
   <input id="img-file-input" type="file" multiple="multiple" accept=".png, .jpg, .jpeg" class="listing-editor__input-img-files" aria-required="true" aria-invalid="false">
   <div class="btn btn--large btn--wide bg--dark-gray">
      <div class="d--fl jc--c ai--c">
         <i class="l-icon cloud m--r--2"></i>
         <div class="tc--white">UPLOAD PHOTOS</div>
      </div>
   </div>
   <div class="tc--lg">or drag them in</div>
</div>

Upvotes: 0

Views: 431

Answers (2)

Muzzamil
Muzzamil

Reputation: 2881

Try with below locator using ID instead absolute xpath

    myElem = WebDriverWait(driver, delay).until(EC.presence_of_element_located((By.ID, 'img-file-input')))    
    myElem.send_keys("path")

Upvotes: 1

Evgeniy Chiruk
Evgeniy Chiruk

Reputation: 358

You need to send the file to the <input> tag using SendKeys. No need to use pywinauto. You can do it as follows:

try:
    myElem = WebDriverWait(driver, delay).until(EC.presence_of_element_located((By.XPATH, '/html/body/div[1]/main/div[2]/div/div/div[2]/section[1]/div/div[2]/div/label/div/div[1]/div/div')))     
    myElem.click()
    driver.find_element_by_xpath('//input[@id='img-file-input']').SendKeys("filePath")
except TimeoutException:
    errorDuringFill = True

Upvotes: 0

Related Questions