Search code examples
pythonselenium-webdriveropendialog

Manage Open Dialog in python


Is there a way to interact with an open dialog opened with Chrome in order to select and open a specific path? I was thinking to use selenium but when I open the open dialog on Chrome I only see this coding once the open dialog is closed:

<form style="display: none;"><input type="file" multiple=""></form>

while I'm expecting to see for example a label related to "File Names" (where you can insert the path of the file) or "Open" for the open-button.

<html><head>
        <meta charset="UTF-8">
        <meta name="author" content="Christoph Lukas Lindtner">
        <meta name="designer" content="Christoph Lukas Lindtner">
        <meta name="description" content="eiCAB - This is 4G">
        <link rel="manifest" href="manifest.json">
        <link rel="icon" href="/img/app/icons/favicon.png" type="image/png">
        <meta name="robots" content="noindex, nofollow">
        <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
        <meta name="mobile-web-app-capable" content="yes">
        <title>HMI8462-TW • C0118828</title>
            <link rel="stylesheet" href="cbd179784e6e85707d5d648d84e84f25732d1e7e1594916ea75f3de455036850.css" type="text/css">
            <script type="application/javascript" src="4fa2c0563949d618857f54c5c23b5e66caad69ba2dc66ccaeeeb0e3fc7b622df.js"></script>
        <script src="blob:http://10.80.100.10/01e10633-12d0-44c1-ba3e-f66c7fe63943"></script></head>
    <body class="st connected" role="application" id="madpenguin"><div role="progressbar" class="orbit" aria-hidden="true" style="display: none;"><canvas width="80" height="80"></canvas></div><div class="ui-workplace-overlay"></div><div role="main" style="height: 872px; width: 314px;"><div role="toolbar" class="action-bar" style="width: 314px;"><div role="button" class="action-control-box"><div class="label" aria-hidden="true" style="display: none;"></div><div role="img" class="action-back" style="transform: translate3d(0px, 0px, 0px);"><svg xmlns="http://www.w3.org/2000/svg" version="1.1" x="0" y="0" width="24" height="24" viewBox="0 0 24 24" xml:space="preserve"><path d="M18.5 12.5H7.3l5.2 5.4 -0.6 0.7L5.5 12l6.3-6.5 0.6 0.7 -5.2 5.4h11.2V12.5z" fill="#878787"></path></svg></div><div role="img" class="action-bar-icon"></div><div role="img" class="action-drawer-control" style="transform: translate3d(24px, 0px, 0px); top: 18px;"><svg xmlns="http://www.w3.org/2000/svg" version="1.1" x="0" y="0" width="24" height="24" viewBox="0 0 24 24" enable-background="new 0 0 24 24" xml:space="preserve"><path fill="#95A5A6" d="M18.5 13h-13v-2h13V13zM18.5 17h-13v2h13V17zM18.5 5h-13v2h13V5z"></path></svg></div></div><div class="action-bar-title" role="heading" aria-level="1" style="width: 51px;"></div><div role="menu" class="action-menu" aria-level="1" aria-posinset="1" style="width: 47px; z-index: 651;"></div><div class="notification-bar"><div role="button" class="widget link online"><div role="img" class="icon"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" id="Ebene_1" x="0px" y="0px" width="24px" height="24px" viewBox="0 0 24 24" style="enable-background:new 0 0 24 24;" xml:space="preserve">
<style type="text/css">
<![CDATA[
    .st0{fill:#9E9E9E;}
]]>
</style>
<g>
    <g>
        <path class="st0" d="M14.112,6.34c1.017-0.6,2.331-0.484,3.181,0.367c0.955,0.954,0.988,2.493,0.116,3.539l0.01,0.009    l-0.178,0.178l-3.377,3.376l-0.023,0.023c-1.041,1.042-2.713,1.065-3.726,0.054c-0.57-0.57-0.812-1.349-0.728-2.107l-2.239,2.239    c0.18,0.551,0.463,1.064,0.852,1.517l0.233,0.233c1.883,1.884,5.137,1.694,7.253-0.422l0.316-0.316l3.019-3.018    c2.087-2.087,2.252-5.317,0.368-7.201c-1.883-1.883-5.113-1.718-7.2,0.369l-1.24,1.24C11.868,6.067,13.04,6.038,14.112,6.34z"></path>
    </g>
    <g>
        <path class="st0" d="M9.887,17.66c-1.016,0.6-2.33,0.483-3.18-0.367C5.752,16.339,5.719,14.8,6.59,13.754l-0.009-0.009    l0.178-0.178l3.376-3.376l0.023-0.023c1.042-1.042,2.713-1.065,3.726-0.053c0.57,0.57,0.812,1.349,0.729,2.107l2.239-2.239    c-0.18-0.551-0.463-1.065-0.852-1.517l-0.233-0.233c-1.884-1.883-5.137-1.695-7.253,0.422L8.199,8.97L5.18,11.988    c-2.087,2.087-2.252,5.317-0.369,7.2c1.884,1.884,5.114,1.719,7.201-0.368l1.24-1.241C12.132,17.933,10.96,17.962,9.887,17.66z"></path>
    </g>
</g>
</svg></div><div class="label" aria-hidden="true" style="display: none;"></div></div><div role="button" class="widget-notifications widget"><div role="img" class="icon"><svg xmlns="http://www.w3.org/2000/svg" version="1.1" x="0" y="0" width="24" height="24" viewBox="0 0 24 24" xml:space="preserve"><path d="M10.2 18.7c0 0 0 0 0 0 0 1 0.8 1.8 1.8 1.8 1 0 1.8-0.8 1.8-1.8 0 0 0 0 0 0 -1 0-1.8 0-1.8 0S11.2 18.7 10.2 18.7zM19.5 16.6c-0.4-1.1-1.6-2.4-2.5-3.8 0 0 0-0.1 0-0.1 0-4.2 0.2-5.2-1.1-7.2C14.6 3.5 12 3.5 12 3.5s-2.6 0-3.9 2c-1.3 2-1 3-1.1 7.2 0 0 0 0.1 0 0.1 -0.8 1.4-2 2.8-2.5 3.8 -0.1 0.3 0 1 0.6 1.1 0.4 0 13.2 0 13.6 0C19.4 17.6 19.6 16.9 19.5 16.6z" fill="#E0E0E0"></path></svg></div><div class="label" aria-hidden="true" style="display: none;"></div></div></div></div><div class="application-background"></div><div style="height: 812px; width: 314px;"><div role="form" class="update-layout vertical" style="overflow: auto; height: 812px; width: 314px;"><div><div role="heading" aria-level="1" class="vertical">Update device software</div><div class="notes paragraph vertical"></div><div class="sw-update-info paragraph vertical">The configured device type is a crucial variable in the software update process. Make sure that the software update you want to deploy is intended for the configured device type.</div><div class="paragraph vertical">Select one or more packages and perform a manual update</div><div role="list" class="software-packages vertical"></div><div class="actions vertical"><div role="button" class="action-select-file standard grey"><div role="img" class="icon"><svg xmlns="http://www.w3.org/2000/svg" version="1.1" x="0" y="0" width="24" height="24" viewBox="0 0 24 24" xml:space="preserve"><path fill="#9E9E9E" d="M6.7 6.4v10.8h10.8V6.4H6.7zM15.1 14.8H9V8.7h6.1V14.8zM9.5 5.5V3.7c0-0.2-0.2-0.4-0.4-0.4 -0.2 0-0.4 0.2-0.4 0.4v1.8H9.5zM15.5 5.5V3.7c0-0.2-0.2-0.4-0.4-0.4 -0.2 0-0.4 0.2-0.4 0.4v1.8H15.5zM12.5 5.5V3.7c0-0.2-0.2-0.4-0.4-0.4 -0.2 0-0.4 0.2-0.4 0.4v1.8H12.5zM14.7 18v1.8c0 0.2 0.2 0.4 0.4 0.4 0.2 0 0.4-0.2 0.4-0.4v-1.8H14.7zM8.6 18v1.8c0 0.2 0.2 0.4 0.4 0.4 0.2 0 0.4-0.2 0.4-0.4v-1.8H8.6zM11.7 18v1.8c0 0.2 0.2 0.4 0.4 0.4 0.2 0 0.4-0.2 0.4-0.4v-1.8H11.7zM5.8 14.4h-1.8c-0.2 0-0.4 0.2-0.4 0.4s0.2 0.4 0.4 0.4h1.8V14.4zM5.8 8.3h-1.8c-0.2 0-0.4 0.2-0.4 0.4 0 0.2 0.2 0.4 0.4 0.4h1.8V8.3zM5.8 11.3h-1.8c-0.2 0-0.4 0.2-0.4 0.4s0.2 0.4 0.4 0.4h1.8V11.3zM18.4 9.1h1.8c0.2 0 0.4-0.2 0.4-0.4 0-0.2-0.2-0.4-0.4-0.4h-1.8V9.1zM18.4 15.2h1.8c0.2 0 0.4-0.2 0.4-0.4s-0.2-0.4-0.4-0.4h-1.8V15.2zM18.4 12.2h1.8c0.2 0 0.4-0.2 0.4-0.4s-0.2-0.4-0.4-0.4h-1.8V12.2z"></path></svg></div><div class="label" style="">Select files</div></div><div role="button" class="action-reboot standard grey" aria-disabled="true"><div role="img" class="icon"><svg xmlns="http://www.w3.org/2000/svg" version="1.1" x="0" y="0" width="24" height="24" viewBox="0 0 24 24" enable-background="new 0 0 24 24" xml:space="preserve"><path fill="#95A5A6" d="M12.7 13.2h-1.2V3.5h1.2V13.2zM15.4 3.5v1.6c0.3 0.2 0.5 0.3 0.8 0.5l0.6-1.3C16.4 4 15.9 3.7 15.4 3.5zM18.5 8.7l1.3-0.4c-0.2-0.5-0.4-0.9-0.6-1.3l-1.1 0.9C18.3 8.2 18.4 8.4 18.5 8.7zM19.1 10.6l1.4 0.2c0-0.5-0.1-1-0.3-1.5l-1.3 0.4C18.9 9.9 19 10.2 19.1 10.6zM19.1 11.6c0 0 0 0 0 0 0 0.4 0 0.8-0.1 1.1l1.2 0.9c0.1-0.6 0.2-1.2 0.2-1.8L19.1 11.6zM12 19.1c-3.9 0-7.1-3.3-7.1-7.4 0-2.8 1.5-5.3 3.7-6.5V3.5c-3 1.4-5.1 4.5-5.1 8.1 0 4.9 3.8 8.9 8.5 8.9 3.7 0 6.8-2.4 8-5.8l-1.2-0.8C17.9 16.9 15.2 19.1 12 19.1zM17.6 7.1l1.1-0.9c-0.3-0.4-0.7-0.8-1.1-1.2l-0.6 1.3C17.2 6.6 17.4 6.8 17.6 7.1z"></path></svg></div><div class="label" style="">Reboot device</div></div></div></div></div></div></div><div class="notification-hub" style="overflow: auto; height: 872px; width: 350px;"><div class="label title-bar"></div><div role="button" class="action-close"><div role="img" class="icon"><svg xmlns="http://www.w3.org/2000/svg" version="1.1" x="0" y="0" width="24" height="24" viewBox="0 0 24 24" xml:space="preserve"><path d="M17.9 17.2c0.2 0.2 0.2 0.5 0 0.7C17.8 18 17.7 18 17.5 18s-0.2 0-0.3-0.1L12 12.7l-5.2 5.2C6.7 18 6.6 18 6.5 18c-0.1 0-0.2 0-0.3-0.1 -0.2-0.2-0.2-0.5 0-0.7l5.2-5.2L6.1 6.8C6 6.6 6 6.3 6.1 6.1 6.3 6 6.6 6 6.8 6.1l5.2 5.2 5.2-5.2c0.2-0.2 0.5-0.2 0.7 0 0.2 0.2 0.2 0.5 0 0.7L12.7 12 17.9 17.2z" fill="#95a5a6"></path></svg></div><div class="label" aria-hidden="true" style="display: none;"></div></div><div></div></div><div role="dialog" class="network-error red"><div role="form"><div role="heading" aria-level="1" class="vertical"></div><div class="paragraph vertical"></div><div class="failed paragraph vertical" aria-hidden="true" style="visibility: hidden;"></div></div><div class="actions"><div role="button" class="action-refresh standard"><div class="label" aria-hidden="true" style="display: none;"></div></div></div></div><div class="action-drawer" style="overflow: hidden;"><div><div class="profile-box"><div role="img"><svg xmlns="http://www.w3.org/2000/svg" version="1.1" x="0" y="0" width="24" height="24" viewBox="0 0 24 24" xml:space="preserve" style="width: 50px; height: 50px;"><path d="M14 12.7c-0.6 0.3-1.2 0.5-2 0.5 -0.7 0-1.3-0.2-1.9-0.5 -2.5 2-5.3 5.5-4.7 6.9 0.6 1.2 12.5 1.2 13 0C19.1 18.4 16.5 14.6 14 12.7zM8.2 9.7c0.5 1.7 2 3 3.8 3 1.8 0 3.3-1.2 3.8-3H8.2zM16.6 9.2c0 0 0-0.9-0.3-1.2 0 0-0.1 0-0.2-0.1 -0.1 0-0.3-0.1-0.3-0.3 0-0.3-0.1-2.5-2.4-3.3 0 0-0.3 0.3-0.6 2.7l0-3.2c0 0 0-0.2-0.1-0.3C12.5 3.5 12.5 3.5 12.4 3.5c-0.2 0-0.8 0-0.8 0s-0.1 0-0.1 0c-0.1 0-0.1 0.1-0.1 0.3 0 0.4 0 3.2 0 3.2s-0.4-2.4-0.7-2.7c0 0-2.3 0.6-2.4 3.2C8.3 7.8 8.2 7.8 8 7.9c-0.1 0-0.2 0.1-0.3 0.2C7.4 8.4 7.4 9.2 7.4 9.2h0.7 7.6H16.6z" fill="#808285"></path></svg></div><div class="label info-box">Admin </div></div><div role="menu" class="drawer-menu" aria-level="1" aria-posinset="1" style="z-index: 651; width: 320px;"><div role="menuitem" class="quicklinks-database" id="ACTION_DATABASE"><div role="img" class="icon"><svg xmlns="http://www.w3.org/2000/svg" version="1.1" x="0" y="0" width="24" height="24" viewBox="0 0 24 24" enable-background="new 0 0 24 24" xml:space="preserve"><path fill="#2196F3" d="M12 3.5c-3.6 0-6.5 1-6.5 2.2v3.3c0 1.2 2.9 2.2 6.5 2.2s6.5-1 6.5-2.2V5.7C18.5 4.5 15.6 3.5 12 3.5zM12 7.5c-2.9 0-5.2-0.8-5.2-1.8 0-1 2.3-1.8 5.2-1.8 2.9 0 5.2 0.8 5.2 1.8C17.2 6.7 14.9 7.5 12 7.5zM12 12.2c-3.4 0-6.2-0.9-6.5-2.1C5.5 10.2 5.5 10.3 5.5 10.3v3.3c0 1.2 2.9 2.2 6.5 2.2s6.5-1 6.5-2.2v-3.3c0-0.1 0-0.1 0-0.2C18.2 11.3 15.4 12.2 12 12.2zM12 16.9c-3.4 0-6.2-0.9-6.5-2.1C5.5 14.9 5.5 14.9 5.5 15v3.3c0 1.2 2.9 2.2 6.5 2.2s6.5-1 6.5-2.2v-3.3c0-0.1 0-0.1 0-0.2C18.2 16 15.4 16.9 12 16.9z"></path></svg></div><div class="label">Database Manager</div></div><div role="menuitem" id="ACTION_SETTINGS"><div role="img" class="icon"><svg xmlns="http://www.w3.org/2000/svg" version="1.1" x="0" y="0" width="24" height="24" viewBox="0 0 24 24" xml:space="preserve"><path d="M19.1 12c0-0.4 0-0.8-0.1-1.2l1.5-1.4c-0.2-0.6-0.4-1.1-0.7-1.6l-2 0.1c-0.5-0.7-1-1.2-1.7-1.7l0.1-2c-0.5-0.3-1.1-0.5-1.6-0.7L13.2 5c-0.4-0.1-0.8-0.1-1.2-0.1 -0.4 0-0.7 0-1 0.1 -0.1 0-0.1 0-0.2 0l-0.7-0.8L9.4 3.5c-0.1 0-0.1 0-0.2 0.1 -0.5 0.2-1 0.4-1.5 0.6l0.1 2c-0.7 0.5-1.2 1-1.7 1.7l-2-0.1C3.9 8.3 3.7 8.9 3.5 9.4L5 10.8c-0.1 0.4-0.1 0.8-0.1 1.2s0 0.8 0.1 1.2l-1.5 1.4c0.2 0.6 0.4 1.1 0.7 1.6l2-0.1c0.5 0.7 1 1.2 1.7 1.7l-0.1 2C8.2 20 8.6 20.2 9 20.3c0.1 0.1 0.3 0.1 0.4 0.2l0.2-0.2 1.2-1.3c0.4 0.1 0.8 0.1 1.2 0.1 0.4 0 0.8 0 1.2-0.1l0.8 0.9 0.5 0.6c0.6-0.2 1.1-0.4 1.6-0.7l0-0.2 -0.1-1.8c0.7-0.5 1.2-1 1.7-1.7l1.6 0.1 0.5 0c0.3-0.5 0.5-1.1 0.7-1.6L19 13.2C19.1 12.8 19.1 12.4 19.1 12zM12 17.3c-2.9 0-5.3-2.4-5.3-5.3 0-2.9 2.4-5.3 5.3-5.3 2.9 0 5.3 2.4 5.3 5.3C17.3 14.9 14.9 17.3 12 17.3z" fill="#2196F3"></path></svg></div><div class="label">Settings</div></div><div role="menuitem" id="ACTION_UPDATE_SOFTWARE" class="state-active"><div role="img" class="icon"><svg xmlns="http://www.w3.org/2000/svg" version="1.1" x="0" y="0" width="24" height="24" viewBox="0 0 24 24" enable-background="new 0 0 24 24" xml:space="preserve"><path fill="#2196F3" d="M10.7 20.5l0.9-1.1c-2.5-0.1-4.8-1.4-6.1-3.6 -1.5-2.6-1.3-6 0.6-8.4 0.1-0.2 0.4-0.2 0.6-0.1 0.2 0.1 0.2 0.4 0.1 0.6 -1.7 2.1-1.9 5.1-0.5 7.4 1.1 1.9 3 3 5.1 3.2l-1-0.8 1.2-0.1 1.5 1.3 -1.3 1.5L10.7 20.5zM17.8 16.6c1.9-2.4 2.2-5.7 0.6-8.4 -1.3-2.2-3.6-3.5-6.1-3.6L13.3 3.5l-1.2 0.1 -1.3 1.5 1.5 1.3 1.2-0.1 -1-0.8c2.1 0.2 4 1.4 5.1 3.2 1.4 2.3 1.2 5.3-0.5 7.4 -0.1 0.1-0.1 0.3 0 0.5 0 0 0.1 0.1 0.1 0.1C17.4 16.8 17.6 16.8 17.8 16.6z"></path></svg></div><div class="label">Update Software</div></div><div role="menuitem" id="ACTION_USER_MANAGER"><div role="img" class="icon"><svg xmlns="http://www.w3.org/2000/svg" version="1.1" x="0" y="0" width="24" height="24" viewBox="0 0 24 24" xml:space="preserve"><path fill="#9E9E9E" d="M16.3 19.6c-0.6 1.2-11.8 1.2-12.3 0 -0.6-1.4 2-4.9 4.4-6.9 0.5 0.3 1.1 0.5 1.8 0.5 0.7 0 1.3-0.2 1.9-0.6C14.4 14.6 16.9 18.4 16.3 19.6zM10.2 4.5c-2.1 0-3.8 1.8-3.8 4s1.7 4 3.8 4c2.1 0 3.8-1.8 3.8-4S12.2 4.5 10.2 4.5zM15.7 11.6c-0.6 0.3-1.2 0.6-1.9 0.6 -0.1 0-0.2 0-0.3 0 2.3 2 4.8 5.8 4.2 6.9 0 0.1-0.2 0.2-0.3 0.3 1.4-0.1 2.4-0.4 2.6-0.7C20.6 17.4 18.1 13.6 15.7 11.6zM15.4 8c0 1.5-0.8 2.8-1.9 3.5 0.1 0 0.3 0 0.4 0 2.1 0 3.8-1.8 3.8-4 0-2.2-1.7-4-3.8-4 -0.7 0-1.3 0.2-1.8 0.5C13.9 4.3 15.4 5.9 15.4 8z"></path></svg></div><div class="label">User Management</div></div><div role="menuitem" class="action-logout" id="ACTION_LOGOUT"><div role="img" class="icon"><svg xmlns="http://www.w3.org/2000/svg" version="1.1" x="0" y="0" width="24" height="24" viewBox="0 0 24 24" enable-background="new 0 0 24 24" xml:space="preserve"><path fill="#7F8C8D" d="M14.8 5v1.3c1.8 1 3.1 3.1 3.1 5.4 0 3.4-2.6 6.1-5.9 6.1 -3.2 0-5.9-2.7-5.9-6.1 0-2.3 1.2-4.3 3.1-5.4V5C6.7 6.1 5 8.7 5 11.7c0 4 3.1 7.3 7 7.3 3.9 0 7-3.3 7-7.3C19 8.7 17.3 6.1 14.8 5zM11.6 5h1v8h-1V5z"></path></svg></div><div class="label">Logout</div></div></div></div></div><div role="dialog" class="log-dialog white"><div aria-hidden="true" style="display: none; overflow: auto;"><div><div role="list"></div></div></div><div class="empty-box"><div class="label" aria-hidden="true" style="display: none;"></div></div><div class="actions"><div role="button" class="action-ok standard"><div class="label" aria-hidden="true" style="display: none;"></div></div><div role="button" class="action-cancel standard"><div class="label" aria-hidden="true" style="display: none;"></div></div></div></div><form style="display: none;"><input type="file" multiple=""></form>
<span style="white-space: nowrap; position: absolute; left: -3900px; width: auto; height: auto; z-index: 10000; display: inline-block; font: 21.3333px Arial, Helvetica, sans-serif;">null</span></body></html>

Solution

  • No, you can't interact with the "Open" dialog from Selenium or any web-based automation tool due to security restrictions.

    You can work around it by sending the file path directly to the input element that triggers the file dialog"

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    driver = webdriver.Chrome()
    driver.get('http://replacewithyourownurl.com')
    trigger_element = driver.find_element(By.XPATH, "//input[@type='button'][@value='Select files']")
    trigger_element.click()
    file_input = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.CSS_SELECTOR, "input[type='file']"))
    )
    driver.execute_script("arguments[0].style.display = 'block';", file_input)
    file_input.send_keys('/path/to/your/file.txt')
    

    You can specify the path to the file you want to upload through the send_keys method.

    Take into account that the input element might be hidden, which is why you may see style="display: none". If so make the element visible before sending keys to it.

    *Corrected 😅.