python-3.xseleniumselenium-webdriverfile-uploadfileopendialog

How to check if Open File dialog has been open after pressing a button in a Chrome Browser tab on Python?


I'm trying to automate a process within the OpenSea Create page after having logged in with Metamask, and so far, I have managed to develop a simple program that chooses a particular image file using a path which passes to the Open File dialog "implicitly", here's the code:

import pyautogui
import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

def wait_xpath(code): #function to wait for the xpath of an element to be located
    WebDriverWait(driver, 60).until(EC.presence_of_element_located((By.XPATH, code)))


opt = Options() #the variable that will store the selenium options
opt.add_experimental_option("debuggerAddress", "localhost:9222") #this allows bulk-dozer to take control of your Chrome Browser in DevTools mode.
s = Service(r'C:\Users\ResetStoreX\AppData\Local\Programs\Python\Python39\Scripts\chromedriver.exe') #Use the chrome driver located at the corresponding path
driver = webdriver.Chrome(service=s, options=opt) #execute the chromedriver.exe with the previous conditions

nft_folder_path = r'C:\Users\ResetStoreX\Pictures\Cryptobote\Cryptobote NFTs\Crypto Cangrejos\SANDwich\Crabs'
start_number = 3


if driver.current_url == 'https://opensea.io/asset/create':
    print('all right')
    print('')
    print(driver.current_window_handle)
    print(driver.window_handles)
    print(driver.title)
    print('')
    nft_to_be_selected = nft_folder_path+"\\"+str(start_number)+".png"
    wait_xpath('//*[@id="main"]/div/div/section/div/form/div[1]/div/div[2]')
    imageUpload = driver.find_element(By.XPATH, '//*[@id="main"]/div/div/section/div/form/div[1]/div/div[2]').click() #click on the upload image button
    print(driver.current_window_handle)
    print(driver.window_handles)
    time.sleep(2)
    pyautogui.write(nft_to_be_selected) 
    pyautogui.press('enter', presses = 2)

Output:

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


Solution

  • 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.