Home > Blockchain >  How to check if Open File dialog has been open after pressing a button in a Chrome Browser tab on Py
How to check if Open File dialog has been open after pressing a button in a Chrome Browser tab on Py

Time:01-26

I'm trying to automate a process within the preview0

After checking the URL, the program clicks on the corresponding button to upload a file

preview1

Then it waits 2 seconds before pasting the image path into the Name textbox, for then pressing Enter

preview2

So the file ends up being correctly uploaded to this page.

The thing is, the program above works because the following conditions are met before execution:

  1. The current window open is the Chrome Browser tab (instead of the Python program itself, i.e. Spyder environment in my case)
  2. After clicking the button to upload a file, the Name textbox is selected by default, regardless the current path it opens with.

So, I'm kind of perfectionist, and I would like to know if there's a method (using Selenium or other Python module) to check if there's an Open File dialog open before doing the rest of the work.

I tried print(driver.window_handles) right after clicking that button, but Selenium did not recognize the Open File dialog as another Chrome Window, it just printed the tab ID of this page, so it seems to me that Selenium can't do what I want, but I'm not sure, so I would like to hear what other methods could be used in this case.

PS: I had to do this process this way because send_keys() method did not work in this page

CodePudding user response:

The dialog you are trying to interact with is a native OS dialog, it's not a kind of browser handler / dialog / tab etc. So Selenium can not indicate it and can not handle it. There are several approaches to work with such OS native dialogs. I do not want to copy - paste existing solutions. You can try for example this solution. It is highly detailed and looks good.

  •  Tags:  
  • Related