为什么只有当用户手动按下 chromedriver 实例中的按钮时才会出现这个新的 window 通知?硒相关

Why does this new window notification appear only when the user has manually pressed a button in a chromedriver instance? Selenium related

我有一个简单的 python 程序,它用 chromedriver.exe 打开 this page,然后点击位于该页面右上角的钱包图标,然后点击MetaMask 钱包按钮,在这里:

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

opt.add_argument('--user-data-dir='+r'C:\Users\ResetStoreX\AppData\Local\Google\Chrome\User Data') #Add the user data path as an argument in selenium Options
opt.add_argument("--profile-directory=Default") #Add the profile directory as an argument in selenium Options
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
driver.implicitly_wait(10)
driver.get('https://opensea.io/') #go to the opensea main page.

initial_page = driver.current_window_handle  #make the previous page the current one
WebDriverWait(driver, 3).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="__next"]/div/div[1]/nav/ul/div[2]/li/button'))) #wait for the wallet button to be enabled for clicking
wallet_button = driver.find_element(By.XPATH, '//*[@id="__next"]/div/div[1]/nav/ul/div[2]/li/button')
wallet_button.click() #click that wallet button
#time.sleep(1)
wallet_options = driver.find_elements(By.XPATH, "//*[@data-testid='WalletSidebar--body']//li")
for i in wallet_options:
    if "MetaMask" in i.get_attribute('innerText'): #find the MetaMask wallet button
        i.click() #click the MetaMask wallet button

我在测试这个程序时注意到的问题是,在通过自动化点击 MetaMask 钱包按钮后,预期的 Metamask Notification window 从未显示,而当前的 window 一直在加载该按钮永远如下所示:

但是,如果用户在同一个 chrome 实例中手动点击同一个 MetaMask 钱包按钮Metamask Notification window 会立即按预期出现,要求用户登录然后将钱包连接到该站点,如下所示:

我什至在使用此代码按下 MetaMask 钱包按钮(通过自动化)后检查是否还有另一个隐藏的 window 句柄,但没有别的:

for handle in driver.window_handles:
            if handle != initial_page:
                login_page = handle
                driver.switch_to.window(login_page)

所以,我很困惑,为什么那个按钮显然只在用户按下时起作用,而在程序按下时却不起作用?一如既往,如果有人能解释发生了什么以及我该如何解决这个问题,我将不胜感激?

我已经弄清楚了,这是一件愚蠢的事情,我只需要添加扩展路径(must be compressed 作为 .crx 文件)作为 Selenium 的 [=12] 的参数=],这里是改进后的代码:

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

opt.add_argument('--user-data-dir='+r'C:\Users\ResetStoreX\AppData\Local\Google\Chrome\User Data') #Add the user data path as an argument in selenium Options
opt.add_argument("--profile-directory=Default") #Add the profile directory as an argument in selenium Options
extension_path = r'C:\Users\ResetStoreX\AppData\Local\Google\Chrome\User Data\Default\Extensions\nkbihfbeogaeaoehlefnkodbefgpgknn.8.1_0.crx' #assign the extension path of the compressed Metamask extension to this variable
opt.add_extension(extension_path) #add the extension_path as argument

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
driver.implicitly_wait(10)
driver.get('https://opensea.io/') #go to the opensea main page.

initial_page = driver.current_window_handle  #make the previous page the current one
WebDriverWait(driver, 3).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="__next"]/div/div[1]/nav/ul/div[2]/li/button'))) #wait for the wallet button to be enabled for clicking
wallet_button = driver.find_element(By.XPATH, '//*[@id="__next"]/div/div[1]/nav/ul/div[2]/li/button')
wallet_button.click() #click that wallet button
#time.sleep(1)
wallet_options = driver.find_elements(By.XPATH, "//*[@data-testid='WalletSidebar--body']//li")
for i in wallet_options:
    if "MetaMask" in i.get_attribute('innerText'): #find the MetaMask wallet button
        i.click() #click the MetaMask wallet button

因此当您编译增强代码时,它会生成以下输出:

我注意到这个程序最终打开了 一个 chromedriver 选项卡一个新的 window 来访问 Metamask钱包(即 两者用于同一事物)。

如果有人想出如何防止 MetaMask 选项卡如图所示打开,请在评论中告诉我。