How to close native file chooser window during Choose File
simulation in Robot Framework?
Hi everyone!
The main issue I’m trying to solve is:
After clicking the “Import” button, I need to upload a file. However, things get tricky because the standard
<input type="file">
element doesn’t appear at all.
**Instead, the file is uploaded via an AJAX request using Dropzone, and the result is stored in a hidden input field like this:
<input type="hidden" name="multilang_image-0-text" value="temp_files/bb255b88-bacf-494c-ab58-8397a0bf743a.jpeg" id="multilang_image-0-undefined">
** So the usual
Choose File
keyword from SeleniumLibrary doesn’t work as expected. **
Hypothesis 1:
Using
Press Keys None ESC
doesn’t work — naturally — because pressing physical keyboard keys is outside the scope of SeleniumLibrary.
I’m using robotframework-seleniumlibrary, so this limitation applies.
Hypothesis 2:
I tried using xdotool and a custom Python function to simulate pressing the Escape key. It works, but:
- On Gitlab CI/CD, I have to install packages like this in my job:
apt-get update && apt-get install -y xvfb xdotool
- Running the same test locally fails with error:
FileNotFoundError: [Errno 2] No such file or directory: 'xdotool'
Here is my custom Python function:
import os
import subprocess
def press_escape_key():
display = os.environ.get("DISPLAY", ":99")
try:
subprocess.run(["xdotool", "key", "Escape"], check=True, env={"DISPLAY": display})
except subprocess.CalledProcessError as e:
raise RuntimeError(f"Failed to send Escape key: {e}")
Additional Context
If anyone is interested in the structure of the image upload widget itself, here is an example of a widget after an image has already been uploaded:
<div class="image-simple-widget js-image-simple-widget" style="position: relative" data-image-settings="{"aspect_ratio":null,"aspect_ratio_tolerance":0.03,"min_height":0,"max_height":0,"max_width":0,"min_width":0,"max_size":10485760,"allowed_types":["image/png","image/jpeg","image/jpg"]}" data-url="/admin/upload_image">
<div id="text" class="image-simple-widget-content dropzone-container">
<input type="hidden" name="multilang_image-0-text" value="temp_files/bb255b88-bacf-494c-ab58-8397a0bf743a.jpeg" id="multilang_image-0-undefined">
<div class="image js-image" style="background-image: url("/admin/get_temp_file/temp_files/bb255b88-bacf-494c-ab58-8397a0bf743a.jpeg");">
<a class="js-full-image-link" href="/admin/get_temp_file/temp_files/bb255b88-bacf-494c-ab58-8397a0bf743a.jpeg" target="_blank">
<i class="glyphicon glyphicon-eye-open"></i>
</a>
<span class="btn btn-sm btn-danger js-clear-image-btn clear-image-btn">
<i class="glyphicon glyphicon-trash"></i>
</span>
</div>
<div class="image-placeholder js-image-placeholder hidden">
<hr class="cross-line">
<hr class="cross-line">
<span class="btn btn-primary upload-btn js-upload-btn">
<i class="glyphicon glyphicon-upload"></i> Upload image
</span>
</div>
<div class="dropzone js-dropzone"></div>
</div>
</div>
Question:
Has anyone found a simpler or more cross-platform way to simulate a physical ESC
key press than using xdotool
?
I’ve also checked this related question on the forum, but couldn’t find a working solution there.
Great thanks in advance!
P.S.