i. Create a new directory for your project.
ii. Open a terminal, navigate to your project directory, and run npm init
to initialize a new Node.js project.
iii. Follow the prompts to set up your project's package.json file.
In the terminal or command prompt, run npm install puppeteer
to install Puppeteer and its dependencies.
-
The code imports the Puppeteer library using the
require
function. -
The code then defines an asynchronous function using the
async
keyword. -
Inside the function, it launches a Puppeteer browser with the
puppeteer.launch
function. It sets theheadless
option tofalse
to show the browser window, and sets theargs
option to['--start-maximized']
to start the browser window in maximized mode. -
It creates a new page using the
browser.newPage
function. -
It sets the viewport of the page to a width of 1366 pixels and a height of 768 pixels using the
page.setViewport
function. -
It navigates to the URL 'https://swap.defillama.com' using the
page.goto
function. -
It waits for the Chain Field with the selector 'input[id="react-select-2-input"]' to appear on the page using the
page.waitForSelector
function with a timeout of 5000 milliseconds. -
It types the text 'Arbitrum One' into the input element using the
page.type
function. -
It presses the 'Enter' key on the keyboard using the
page.keyboard.press
function. -
It finds the "You Sell" Text Field with class 'css-79elbk' and the type 'text' using the
page.$
function. -
It clicks on the input element by calling the
click
function on the element with the option{ clickCount: 3 }
. This selects the existing text in the input element. -
It types the text '12' into the input element using the
page.type
function. -
It defines an asynchronous function called
clickButton
that takes an argumentbuttonIndex
. -
Inside the
clickButton
function, it uses thepage.evaluate
function to execute a JavaScript code in the context of the page. The code selects all the buttons on the page and clicks on the button at the specified index. -
It calls the
clickButton
function with argument 3 to select the "You Sell" token. -
It waits for 500 milliseconds using the
setTimeout
function. -
It types the text 'Wrapped BTC' into an input element with the class 'chakra-input css-s1d1f4' using the
page.type
function. -
It waits for 500 milliseconds using the
setTimeout
function. -
It defines an asynchronous function called
selectToken
that takes an argumenttokenName
. -
Inside the
selectToken
function, it uses thepage.evaluate
function to execute a JavaScript code in the context of the page. The code selects all the tokens on the page and clicks on the token that matches the specified name. -
It calls the
selectToken
function with the argument 'Wrapped BTC (WBTC)'. -
It calls the
clickButton
function with argument 5 to select the "You Buy" token. -
It waits for an input element with the class 'chakra-input css-s1d1f4' to appear on the page using the
page.waitForSelector
function with a timeout of 1000 milliseconds. -
It types the text 'USDC' into the input element using the
page.type
function. -
It waits for 500 milliseconds using the
setTimeout
function. -
It calls the
selectToken
function with the argument 'USD Coin (USDC)'. -
It waits for the Routing List with class 'sc-55ee4011-2 fcGAPg' to appear on the page using the
page.waitForSelector
function with a timeout of 3000 milliseconds. -
It waits for 4000 milliseconds using the
setTimeout
function. -
It uses the
page.evaluate
function to execute JavaScript code in the context of the page. The code selects second option from a list of options and clicks on it. -
The code is immediately invoked by calling the asynchronous function using the
()
syntax.