DEV Community

Leonardo Teixeira Menezes
Leonardo Teixeira Menezes

Posted on

Automated Dapps Scrapping with Metamask and C#

Hey everyone!
This article is a follow up on my last article about Daaps Scrapping, if you have not read it yet I suggest heading to:

Thank you all for the great feedback you provided in the last article, it's great to see people building their automations and sharing their own challenges.

One of my followers requested an example of how Metamask automation would look like in C#, so a built an automation for the same 'Import Wallet' flow:

static void Main(string[] args)
{
        ChromeOptions options = new ChromeOptions();
        options.AddExtension(EXTENSION_PATH);
        IWebDriver driver = new ChromeDriver(CHROME_PATH, options);

        // Navigate to Metamask extension page
        driver.Navigate().GoToUrl($"chrome-extension://{EXTENSION_ID}/popup.html");
        Thread.Sleep(1000);

        // Enter Metamask import wallet flow
        driver.FindElement(By.XPath("//button[text()=\"Get Started\"]")).Click();
        driver.FindElement(By.XPath("//button[text()=\"Import wallet\"]")).Click();
        driver.FindElement(By.XPath("//button[text()=\"No Thanks\"]")).Click();
        Thread.Sleep(1000);

        // Send recovery phrase, password to import the wallet
        IList<IWebElement> inputFields = driver.FindElements(By.XPath("//input")).ToList();
        inputFields[0].SendKeys(SECRET_RECOVERY_PHRASE);
        inputFields[1].SendKeys(NEW_SECRET);
        inputFields[2].SendKeys(NEW_SECRET);
        driver.FindElement(By.CssSelector(".first-time-flow__terms")).Click();
        driver.FindElement(By.XPath("//button[text()=\"Import\"]")).Click();
        Thread.Sleep(2000);

        driver.FindElement(By.XPath("//button[text()=\"All Done\"]")).Click();
        Thread.Sleep(3000);

        // Close Metamask 'What's new' popup
        driver.FindElement(By.CssSelector(".popover-header__button")).Click();
        Console.ReadLine();
}
Enter fullscreen mode Exit fullscreen mode

The concepts used here are the same as the previous article, we load Metamask extension to our ChromeDriver, navigate to its extension page and then automate the import flow as we would with any webpage.

It's good to note that the XPaths and CSS selectors used here might differ based on your Metamask extension version, so if you have issues in any of these steps jump over to your local ChromeDriver window and debug which selector broke the automation.

Let me know in the comments for which Dapp you are building your automation for and which challenges you're facing, I will make sure to address them in the next article!

Top comments (2)

Collapse
 
vishwas02 profile image
Vishwas-02

Hey man thanks for this , but I am stuck at a point . While automating Dapp , I need to connect wallet by clicking "connect wallet" button . This automatically calling extension popup of Metamask . Now how to move into that popup extension and operate wallet for Trasanctions .

Collapse
 
vishwas02 profile image
Vishwas-02

Edit- I am using selenium -Java