[英]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当我点击上传照片时 - 弹出 Windows 窗口并要求用户选择图片
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也尝试过pyWinAuto和pyAutoIt
Wasn't able to accomplish it.没能实现。
Using:使用:
webdriver.Firefox()
Edit HTML:编辑 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
尝试使用以下定位器使用
ID
而不是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
.您需要使用
SendKeys
将文件发送到<input>
标签。 No need to use pywinauto .无需使用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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.