Upload photo in selenium python when input to file is not displayed

This is the html code for uploading the photo:

<div id="choose-photo" class="controls avatar-settings inline-upload-avatar dropdown center"> <div class="uploader-image uploader-avatar clearfix"> <div class="dropdown-menu"> <div class="dropdown-caret"> <span class="caret-outer"></span> <span class="caret-inner"></span> </div> <ul tabindex="-1" role="menu" aria-hidden="true"> <li id="photo-choose-existing" class="photo-choose-existing upload-photo" role="presentation"> <button type="button" class="dropdown-link" role="menuitem">Prześlij zdjęcie</button> <div class="photo-selector"> <button class="btn" type="button"> Zmień zdjęcie </button> <span class="photo-file-name">Nie wybrano pliku</span> <div class="image-selector"> <input type="hidden" name="media_file_name" class="file-name"> <input type="hidden" name="media_data_empty" class="file-data"> <label class="t1-label"> <span class="u-hiddenVisually">Dodaj zdjęcie</span> <input type="file" name="media_empty" class="file-input js-tooltip" tabindex="-1" accept="image/gif,image/jpeg,image/jpg,image/png" data-original-title="Dodaj zdjęcie"> </label> </div> </div> </li> <li id="photo-choose-webcam" class="u-hidden" role="presentation"> <button type="button" class="dropdown-link">Zrób zdjęcie</button> </li> <li id="photo-delete-image" class="u-hidden" role="presentation"> <button type="button" class="dropdown-link" role="menuitem">Usuń</button> </li> <li class="dropdown-divider" role="presentation"></li> <li class="cancel-options" role="presentation"> <button type="button" class="dropdown-link" role="menuitem">Anuluj</button> </li> </ul> </div> </div> </div> 

I created a simple way to send text for input (it does not appear on the screen):

 fileInput = driver.find_element_by_name('media_empty') fileInput.send_keys(path) 

But he does nothing. Also I do not get any errors.

So here is a second way that might work:

 <div class="ProfileAvatarEditing-buttonContainer"> <button class="ProfileAvatarEditing-button u-boxShadowInsetUserColorHover" type="button" tabindex="2"> <div class="ProfileAvatarEditing-addAvatarHelp"> <span class="Icon Icon--cameraPlus"></span> <p>Dodaj zdjęcie profilowe</p> </div> <div class="ProfileAvatarEditing-changeAvatarHelp"> <span class="Icon Icon--camera"></span> <p>Zmień zdjęcie profilowe</p> </div> <div class="ProfileAvatarEditing-dropAvatarHelp"> <span class="Icon Icon--cameraPlus"></span> <p>Upuść zdięcie profilowe tutaj</p> </div> </button> 

Here the user can draw and delete files. I found this question: Selenium: drag and drop from file system to webdriver? however, I still do not know how I can use it in this situation.

So, the question is how to send the file path to the input to start the file download. In this case, when you select a file from the file dialog box or drag it, you will see a confirmation window with a preview on your photo. So, all that remains to be done is to click “Confirm”. But I do not know how to send it in the first place.

Any help would be appreciated.

edit: I found a solution (my own answer below):

 fileInput = driver.find_element_by_xpath('//*[@id="photo-choose-existing"]/div/div/label/input') fileInput.send_keys(path) 

but there is another problem: the photo is loading, but the file dialog still opens - I do not know how to close it. I tried this:

 dialog = driver.switch_to.active_element['value'] 

but I don’t know how to close it.

+5
source share
3 answers

Oddly enough, I found that send_key really works. When I checked the html code in another browser, it was no longer "media_empty", but a different name ("media []" or something similar). Instead, I used xpath, and I was stunned that it really worked:

 fileInput = driver.find_element_by_xpath('//*[@id="photo-choose-existing"]/div/div/label/input') fileInput.send_keys(path) 
+1
source

Suppose an element is present on the page unless you explicitly wait for the element to exist on the page.

try the following:

 driver.execute_script("document.getElementById('ID_HERE').setAttribute('value', 'PATH_HERE')"); 

Hope this helps you!

0
source

try using the code below:

 fileInput = driver.find_element_by_css_selector("div.image-selector label.t1-label input") driver.execute_script("arguments[0].setAttribute('value', 'YOUR_PATH_HERE')",fileInput) 
0
source

Source: https://habr.com/ru/post/1268054/


All Articles