简体   繁体   English

Python -> Selenium:无法上传文件

[英]Python -> Selenium : Not able to upload the file

Code: <input type="file" accept="image/png, image/jpeg" style="display: none;">代码: <input type="file" accept="image/png, image/jpeg" style="display: none;">

What I am trying to do:我正在尝试做的事情:

upload = driver.find_elements_by_xpath("//input[@type = 'file']") upload.send_keys('/Users/username/Desktop/images/filename.jpg') upload = driver.find_elements_by_xpath("//input[@type = 'file']") upload.send_keys('/Users/username/Desktop/images/filename.jpg')

PS: This code is not in any iframe and element can be found (I checked its attribute to be sure) PS:此代码不在任何 iframe 中,并且可以找到元素(我检查了它的属性以确定)

Nothing happens, I have written the same logic to upload files in other test cases and it worked.没有任何反应,我已经编写了相同的逻辑来在其他测试用例中上传文件并且它有效。

Thanks for your help!谢谢你的帮助!

I was able to figure out the issue.我能够弄清楚这个问题。 Below the input field accepts "jpeg and png" and I was trying to upload "jpg".输入字段下方接受“jpeg and png”,我试图上传“jpg”。 Once I changed the input to "jpeg or png" it worked.一旦我将输入更改为“jpeg 或 png”,它就起作用了。

Code:代码:

<input type="file" accept="image/png, image/jpeg" style="display: none;">

What I was doing:我在做什么:

upload.send_keys('/Users/username/Desktop/images/filename.jpg')

Solution: Change the file extension to jpeg or png.解决方案:将文件扩展名更改为 jpeg 或 png。

upload.send_keys('/Users/username/Desktop/images/filename.jpeg')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM