Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to upload file using selenium webdriver+python

I have to upload file (drop zone -> click ->open window to choose file)

I've tried:

addphoto.send_keys("C:\\files\\file.jpg")

But it doesn't work. Is there any robot to handle with new window opened?

like image 811
Basia Avatar asked Dec 25 '22 00:12

Basia


2 Answers

Putting a file name in dropzone hidden input works fine. This should get you going.

upload_file = driver.find_element_by_css_selector('.dz-hidden-input')
data_file = Path(__file__).parent / "test_file.txt"
logging.debug("data_file: %s", data_file)
assert data_file.exists()
upload_file.send_keys(str(data_file))
assert driver.find_element_by_css_selector('.dz-image').is_displayed()
like image 122
Janusz Skonieczny Avatar answered Jan 11 '23 13:01

Janusz Skonieczny


I did it! Just pip install -U pyautoit

then import autoit autoit.win_wait_active("File Upload", 5) autoit.send(os.path.join("path")) autoit.send("{ENTER}")

Works ok :)

like image 31
Basia Avatar answered Jan 11 '23 12:01

Basia