简体   繁体   中英

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>

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

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

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