问题
My automation script needs to upload an image to the webpage but I can't seem to upload the image using the normal send_keys method. I suspect it has something to do with the Angular components but I'm not sure how to access them using Selenium webdriver.
I have tried the following: Automate file upload using Selenium and ng-file-upload and How to upload file using python+selenium?
These don't seem to have the solution I'm looking for.
<button class="md-raised choose-file md-button md-ink-ripple ng-empty ng-valid" type="button" ng-transclude="" ngf-select="" accept="image/*" ng-model="vm.uploader.original" aria-invalid="false">Choose file</button>
I don't have any errors because I can successfully locate the element but the image is not uploaded/sent.
file_input.send_keys("/location/of/image/profile_student.jpg")
回答1:
To handle windows object you can use autoit
However there is Python binding for AutoItX3.dll
You need to install PyAutoIt
using pip.
pip install -U pyautoit
You need to import autoit on your python scripts.
import autoit
Click on the chose file
button first.
driver.find_element_by_xpath("//button[@type='button'][contains(.,'Choose file')]").click()
Then add the following code with the relevant file path
filepath="C:\\filelocation\\filename.jpg"
autoit.win_wait_active("File Upload",5)
if autoit.win_exists("File Upload"):
autoit.control_send("File Upload","Edit1",filepath+"{ENTER}")
Let me know if you need further assistance.
来源:https://stackoverflow.com/questions/56166262/how-to-upload-image-with-angular-components-using-python-selenium