Automate your web pages with "webbot 🤖" without much code 🚀🔥
Tags: octograd2020, webdev, python, automation
Published: 2020-05-21 17:19:39

## What is webbot ? 
webbot is a web automation library in Python. To be more descriptive , it a library used for web browser automation and to some extent can be used for ui automation and end to end testing. But why should you use this when clearly there are other alternatives 😄

## Why you may want to use webbot ?
Here are some of the features that I have listed on my github page which I am directly quoting here : 

- Use any combination of selectors like id, name, text, css etc to perform actions on elements with one line of code.
- Automate and test pages loaded dynamically by javascript.
- Click any button or link without having to worry about finding the element first or knowing css_selectors , xpath etc
- Smart scoring algorithm which finds the best matching elements on which you want to perform the action .
- Automation designed to work even in case of webpages with dynamically changing id and classname
- Immensely minimises the code required for performing input actions like clicks and keyboard actions.
- Get webpage source , cookies , total tabs , webpage title etc..
- Simulate key presses and special key combinations
- Bidirectional scrolling
- Perform an action on webpage elements by applying various filters to select the elements .
- Perform action on multiple elements at once.
- If certain elements don't have text or visible property , then use either their id , class , css_selector , xpath , tag etc.
- The entire automation process can be made without having to open the browser window i.e in the background as a console process (see docs for more details)
 

## Link to Code

{% github https://github.com/nateshmbhat/webbot %}

### Here's a sample code snippet of webbot under action 🥳

The below code will first go to google.com and search for the song "hello its me" and then come back to google and signs in with the provided email and password.

```python
from webbot import Browser 
web = Browser()
web.go_to('google.com') 
web.type('hello its me')  # or web.press(web.Key.SHIFT + 'hello its me')
web.press(web.Key.ENTER)
web.go_back()
web.click('Sign in')
web.type('mymail@gmail.com' , into='Email')
web.click('NEXT' , tag='span')
web.type('mypassword' , into='Password' , id='passwordFieldId')
web.click('NEXT' , tag='span')
``` 

If you wish to use some lower level "find_element_by..." functions yourself , you are still covered 😁. Just get the underlying selenium driver object like this : `web.driver`


## Why and How I made it ?

Well , honestly it was mostly about writing too much code when using selenium for web automation. 

I was using selenium for some of my projects and it felt like I was writing way too much code than what was necessary. I realised that the reason this happened was not because of any inefficiency or bad api of selenium but it was just because of the way in which the interaction with the web was modelled upon.

What ? What do I mean by this ?

- The web automation code first requires that the element is first found before we perform any action.
- This means that we end up writing too much "find_element_by_..." code rather than focusing on just doing what we wanted : i.e just click that damn button which just said "click me".
- So basically we first find the element which we were looking for and then perform the action. The action could be a click or a keyboard keypress , scroll etc
- But what we as developers want is to just perform the action on some element not worry about finding it all by ourselves.
- In a nut shell , what we want is an action centric approach rather than an element centric approach.

With webbot , that's the approach I am taking. An action centric approach rather than the usual element centric approach. You tell webbot what action you wanna do rather than trying to worry about all those css selectors , xpath , etc. Depending on the type of action you perform , whether it is click , type , keypress etc , it has a scoring algorithm that will find appropriate elements based on the action. 

So for example , if you are trying to run `web.click( 'sign in' )` , then it gives a scoring to all elements containing the 'sign in' text and the clickable elements (buttons , anchor tags , elements with onPress) etc take appropriate scores and the best scoring element from the lot is chosen to perform the click action.  


## Additional Thoughts / Feelings / Stories

I made this project during my college days. This is my graduation year. Loads and loads of memories to cherish. Now, when I look back , I realise that these were the best days of my life. These were the days where I lived more than I had ever ! Will miss all this so damn much. Anyway I am writing this article because I got an email from GitHub regarding a "graduation" ceremony for 2020 graduates. I am more than happy to take part in this :)

Thanks guys :)

You can check my other post on dev I wrote earlier here where I talk about using electron with react and typescript support : 

[Terminal Tweaker : An electron-react project to tweak the new windows terminal to heart's content ❤.](https://dev.to/nateshmbhat/terminal-tweaker-an-electron-react-project-to-tweak-the-new-windows-terminal-to-heart-s-content-4c95)



You can check me out on :
Github : https://github.com/nateshmbhat
LinkedIn : https://www.linkedin.com/in/nateshmbhat
Instagram : https://www.instagram.com/nateshmbhat
Facebook : https://www.facebook.com/profile.php?...
Twitter : https://twitter.com/nateshmbhat


### Thank You for reading till this last line 😍 !!! You are amazing 😎!