Metamask でログインした後、 OpenSea の作成ページ内のプロセスを自動化しようとしていますが、これまでのところ、[ファイルを開く] ダイアログに暗黙的に渡されるパスを使用して特定の画像ファイルを選択する簡単なプログラムを開発することができました。"、コードは次のとおりです。
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)
出力:
URL を確認した後、プログラムは対応するボタンをクリックしてファイルをアップロードします。
Name
次に、画像パスをテキストボックスに貼り付ける前に2秒間待機します。Enter
したがって、ファイルはこのページに正しくアップロードされます。
問題は、実行前に次の条件が満たされているため、上記のプログラムが機能することです。
- 現在開いているウィンドウはChromeブラウザタブです(Pythonプログラム自体、つまり私の場合はSpyder環境ではありません)
- ボタンをクリックしてファイルをアップロードすると
Name
、現在のパスに関係なく、デフォルトでテキストボックスが選択されます。
だから、私は一種の完璧主義者でありOpen File dialog
、残りの作業を行う前に開いているかどうかを確認する方法 (Selenium または他の Python モジュールを使用) があるかどうかを知りたいです。
print(driver.window_handles)
そのボタンをクリックした直後に試してみましたが、Selenium はOpen File dialog
を別の Chrome ウィンドウとして認識せず、このページのタブ ID を出力しただけなので、Selenium は自分のやりたいことができないように思えますが、よくわかりません、この場合に使用できる他の方法を聞きたいです。
PS:このページではメソッドが機能しなかったため、このプロセスをこの方法で実行する必要がありましたsend_keys()