Automating a website from Chrome DevTools
Published: 2021-12-16 23:02:08

In this tutorial, we are going to create a simple code snippet to automate tasks on a webpage without the need for any external tools or frameworks.

If you find yourself having to repeatedly do the same stuff on a particular website, then you should probably try this. 

Moreover, it is a fun and good way to JavaScript and the DevTools.

You can play around with browser games and try to beat the high score, just the way I got a high score of 400 clicks-per-second (CPS) in a game. [do not tell the police 😂]


![cute police puppy](https://cdn.hashnode.com/res/hashnode/image/upload/v1639692241589/Pv5CQjIJk.webp)

So let's get right into it.

## What are we building

For the purpose of this post, we'd be automating this [https://invoice-generator.com/](https://invoice-generator.com/) website that allows you to create invoices online. Let's assume we rarely change most of the input except these, for a particular client:

- invoice number
- units of hours worked
- date (auto-generated)

![invoice_generator_website.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1639692399759/ejYDy9-7T.png)

## Inspecting JS selector

We will open our DevTools to be able to inspect elements and get a JS selector to access any element we want to work on. For instance, to get the `invoice number` input element, we right-click on the element in the Elements tab in DevTools and select **copy JS path copy**

![copy_js_path (1).png](https://cdn.hashnode.com/res/hashnode/image/upload/v1639692339369/CPP_nP7oo.png)

List of elements we will be interacting with

```javascript
const invoiceNumber = document.querySelector(
  "div.papers > div  div.title > div > div > input"
);
const billTo = document.querySelector(
  "div.papers > div > div:nth-child(1) > div.col.contact-info > div.row.bill-to-details > div:nth-child(1) textarea"
);
const invoiceFrom = document.querySelector(
  "div.papers > div > div:nth-child(1) > div.col.contact-info > div.contact.from textarea"
);
// date format "Dec 9,2021"
const dateInput = document.querySelector("#dp1639600324282");
const itemDescription = document.querySelector(
  "div.papers div.items-holder > div.items-table div.name textarea"
);
const itemQuantity = document.querySelector(
  "div.papers div.items-holder > div.items-table div.quantity > input"
);
const itemRate = document.querySelector(
  "div.papers div.items-holder > div.items-table div.unit_cost input"
);
```

Then we'd create functions to perform the input value operations

```javascript
function promptsForUser() {
  invoiceNumber.value = prompt("Enter the invoic number");
  itemQuantity.value = prompt("Total hours worked");

	itemQuantity.dispatchEvent(new Event("change"))
}

function setConstantValue() {
  let d = new Date();
  const months = ["Jan","Feb","Mar","April","May","June","Aug","Sept","Oct","Nov","Dec",];
  dateInput.value = `${months[d.getMonth() - 1]} ${d.getDate()}, ${d.getFullYear()}`;
  invoiceFrom.value = "Ichigo Kurosaki,\nSubstitute Soul Reaper";
  billTo.value = "XYZ Limited,\nMugen Train District";
	itemDescription.value = "Bankai night shift";
  itemRate.value = "10";
}

promptsForUser()
setConstantValue()
```

The `setConstantConstant` function changes the value of the input elements to values that stay constant mostly through the use of the script and the `promptsForUser` function to retrieve some information from the script user. 
Also, the total amount in the invoice listens to a `change` event from the quantity so we manually dispatched a `change` event.

## Saving our code as snippets in DevTools

DevTools [Snippets](https://developer.chrome.com/docs/devtools/javascript/snippets/) allow you to save your script in the browser and allow reusability

We'd find the Snippet editor from the **Sources** tab in DevTools

![DevTools Snippets](https://cdn.hashnode.com/res/hashnode/image/upload/v1639692457461/I3d3yCZ2o.png)

 Create a new snippet and paste all our code within. 

## Showcase


![example showcase](https://cdn.hashnode.com/res/hashnode/image/upload/v1639692565941/JJJlS6sjw.gif)

## Conclusion

A downside to this approach if you have no control over the website is that the elements selectors can change anytime so this method is not generally advisable for web automation
Still, it's a great way to play around with DevTools. You can inspect **Local Storage, Cookies**, pause the javascript execution and play around with the debugger.

Have fun trying to beat my 400 click-per-second highscore!!!