使用Selenium webdriver实现Metamask自动化

16

我正在尝试访问需要Metamask扩展程序才能访问的Dapp。我通过Chrome扩展程序添加了它。我知道如何在Selenium中将扩展程序添加到Chrome实例中,但我不知道如何添加密码等内容。是否有人可以下载Metamask并给我一个示例,说明如何使用它通过Selenium传递凭据?

 ChromeOptions options = new ChromeOptions();
 options.addArguments("--start-maximized");
 options.addExtensions(newFile("//Applications//chrome//MetaMask_v3.13.8.crx"));                
 driver = new ChromeDriver(options);

你解决了这个问题吗? - Optimworks
2个回答

2

由于Metamask中钱包信息输入过程比较复杂,因此似乎最好的方法是使用预配置的Chrome配置文件启动Chrome并指定配置文件目录,以便与Metamask扩展程序一起使用测试。

# google-chrome -user-data-dir=/tmp/profile

然后添加Metamask扩展并手动配置您的钱包,然后将相应的参数添加到WebDriver选项中,以使用此配置文件而不是创建一个空配置文件:

options.addArguments("user-data-dir=/tmp/profile");

然后,在测试中,您将需要重新输入Metamask的密码,然后您就可以开始了。


0
这是一个简单的例子,展示了如何轻松创建不同的配置文件并加载它们。自动化扩展并不难,你只需像与另一个页面互动一样打开它。
headless_options = Options()
headless_options.add_argument("--start-maximized");
headless_options.add_extension("extension_10_9_0_0.crx");
headless_options.add_argument("user-data-dir=C:/Users/V/Documents/STACKS OVERFLOW/Test")  # Save Profile
headless_options.add_argument("--profile-directory=test")  # Choose witch profile you would like to use
driver = webdriver.Chrome(options=headless_options)
driver.get(
    'chrome-extension://nkbihfbeogaeaoehlefnkodbefgpgknn/home.html#initialize/create-password')  # Go straight to creating Metamaks wallet

driver.switch_to.window(driver.window_handles[1])  # Switch to main tab

driver.find_element(By.ID, "create-password").send_keys("IlovePython")
driver.find_element(By.ID, "confirm-password").send_keys("IlovePython")
driver.find_element(By.CLASS_NAME, "first-time-flow__checkbox").click()

driver.find_element(By.XPATH, "//*[@id=\"app-content\"]/div/div[2]/div/div/div[2]/form/button").click()

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接