Click here to Skip to main content
14,980,603 members
Articles / Programming Languages / Python
Posted 11 Jan 2013


157 bookmarked

Web Automation

Rate me:
Please Sign up or sign in to vote.
4.86/5 (48 votes)
3 Jan 2017CPOL6 min read
You can fill any form, click any button, and extract any data from web by using Web Automation.


Web Automation written in C#, using Web Browser control to automate many things. You can do many things by writing JavaScript code to control a Web Browser.


You must have basic knowledge about JavaScript and C#, because Web Automation uses JavaScript to communicate with C# function.

How It Works

Image 1

This code below explain communication between JavaScript function and C# function. If you don't use it when you call JavaScript to C# function, it will not work.

// This nested class must be ComVisible for the JavaScript to be able to call it.
public partial class frmMain : Form

Then you create a Web Browser and a load JavaScript function on the Web Browser like in the code below on Form Load:

private void frmMain_Load(object sender, EventArgs e)
    wbMain = new WebBrowser();
    wbMain.ObjectForScripting = this;
    wbMain.DocumentText =
        "<html>" +
        "<head>" +
        "<script type='text/javascript'>" +
            "function go(url) " +
            "{ " +
                "window.external.go(url);" +
            "}" +
        "</script>" +
        "</head>" +
        "<body>" +
        "</body>" +

The code below has two important points, first ObjectForScripting and window.external:

The purpose of ObjectForScripting is for communication between JavaScript and the frmMain application, you can replace this by another class if you want, this class will be called from the JavaScript function.

I load the go function in Web Browser when loading the application, the purpose is when you execute script, it will call function go in C#:

public void go(string url)

How you execute the script:

new object[] { "go('');" });  

When you execute the script below, if function is JavaScript it will execute, if not it will find function you load from Web Browser, example function go, it not function JavaScript so that function will call, then window.external.go(url) will communicate with window application by ObjectForScripting and run function go(string url) and it will show dialog with message is '';

Combine all code and you will see how it is working:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Runtime.InteropServices;

namespace WebAutomation
    public partial class frmMain : Form
        private WebBrowser wbMain;

        public frmMain()

        private void frmMain_Load(object sender, EventArgs e)
            wbMain = new WebBrowser();
            wbMain.ObjectForScripting = this;
            wbMain.DocumentText =
                "<html>" +
                "<head>" +
                "<script type='text/javascript'>" +
                    "function go(url) " +
                    "{ " +
                        "window.external.go(url);" +
                    "}" +
                "</script>" +
                "</head>" +
                "<body>" +
                "</body>" +

        public void go(string url)

        private void btnRun_Click(object sender, EventArgs e)
            string code = tbxCode.Text;
            wbMain.Document.InvokeScript("eval", new object[] { code }); 


In my application, I using a method "Call a C# Method From JavaScript Hosted in a WebBrowser" to control anything JavaScript cannot work like writing to file, opening tab, working with file and folder...

Secure sensitive information like Account, User, Email Server, Connection by using "DotNet Zip Library" and "Secure Password Authentication".

Using "Mouse Key Board Library" to record and playback your mouse and your keyboard by script.

Recognize text from image by "Tesseract OCR", read and write Excel file is "NPOI", Speech to Text is "Speech Recognition in C#".


Web Automation has four sections:

  1. Menu file will help you use some features of program like login, logout, register...,
  2. ToolBar menu will help you work with some item you will usually use like record, run,...,
  3. Web Browser will display website when you are suffering,
  4. Developer Tools that place will automate every of program by JavaScript language

Image 2

Using Web Automation to Login Gmail

Open program and enter link and click button go.

Image 3

Right click everywhere on Web Browser, click Go, then will it show question "Do you want go to website?", if you choose yes, it will get current URL you enter on the textbox, else it will get XPath of location where you right click, then go to the link of the xpath.

Image 4

Then it will show you Developer Tools with code JavaScript to control this function.

Image 5

Right click on textbox username, click fill, enter username then click OK, repeat for password.

Image 6

Right click on Button Login, select click.

Image 7

You must enter two lines to ensure website load done before you continue, then you click Run and then program will go to Gmail for you.


What Do You Do with Web Automation?

Here is the function of Web Automation, you can try by writing the function below with your parameter, then click run and see what happen, notes some function need authentication to work like function number: 59 to 64, rest of another function, you don't need to login.

  1. Open new tab: tabnew();
  2. Close current tab: tabclose();
  3. Go to website by url or xpath: go(url); or go(xpath);
  4. Go back: back();
  5. Go next: next();
  6. Reload browser: reload();
  7. Stop browser: stop();
  8. Wait until Web Browser has loaded or timer to wait Web Browser load: sleep(seconds, isBreakWhenWBCompleted)
  9. Close application: exit();
  10. Click to element: click(xpath);
  11. Write log to Preview Tab: log(value);
  12. Clear log write on Preview: clearlog();
  13. Extract data from xpath: extract(xpath);
  14. Fill data to element on Web Browser: fill(xpath, value);
  15. Select dropdown value: filldropdown(xpath, index);
  16. Convert string to Object JavaScript: toObject(xpath);
  17. Get all link in the area of xpath, it will stop until program go all of link: browser(xpath);
  18. Reset list website to unread so program can go back and browser continue: resetlistwebsite();
  19. Take snapshot of website: takesnapshot(url);
  20. Create folder: createfolder(path);
  21. Get list folder from path: getfolders(path);
  22. Create file: save(content, location, isOverride);
  23. Get list file from path: getfiles(path);
  24. Download a file: download(url, location);
  25. Read file: read(path);
  26. Remove file: remove(path);
  27. Remove folder: removefolder(path);
  28. Open explorer and select file: explorer(path);
  29. Run code from string: excute(code);
  30. Logoff: logoff();
  31. Lockworkstation: lockworkstation();
  32. Forcelogoff: forcelogoff();
  33. Reboot: reboot();
  34. Shutdown: shutdown();
  35. Hibernate: hibernate();
  36. Standby: standby();
  37. Run application: runcommand(path);
  38. Get current url of current tab: getcurrenturl();
  39. Get browser height: getheight();
  40. Get title of website: gettitle();
  41. Get all link from xpath: getlinks(xpath);
  42. Get content of web browser: getCurrentContent();
  43. Get path of Web Automation: getCurrentPath();
  44. Read cell of Excel file: readCellExcel(path, sheetname, row, column);
  45. Write Excel file: writeCellExcel(path, sheetname, cellname, value);
  46. Load HTML file: loadHTML(path);
  47. Convert json string to Object: textToJSON(jsonstring);
  48. Login: login(username, password);
  49. Register: register(username, email, password, confirm);
  50. OCR: ocr(path, language);
  51. Mouse Up: MouseUp(mouseButton, lastTime);
  52. Mouse Down: MouseDown(mouseButton, lastTime);
  53. Mouse Click: MouseClick(mouseButton, lastTime);
  54. Mouse Doble Click: MouseDoubleClick(mouseButton, lastTime);
  55. Mouse Move: MouseMove(x, y, isShow, lastTime);
  56. Mouse Wheel: MouseWheel(delta, lastTime);
  57. Key Down: KeyDown(delta, lastTime);
  58. Key Up: KeyUp(delta, lastTime);
  59. Get Account By: getAccountBy(name);
  60. Get Database: getDatabases(name);
  61. Get Tables: getTables(name, dbName);
  62. Get Columns: getColumns(name, dbName, table);
  63. Get Rows: getRows(name, dbName, sql);
  64. Execute SQL Query: excuteQuery(name, dbName, sql);

Those are all the functions of Web Automation. Every function is a JavaScript function so it have return or not return value, sometimes you get value as string, sometime you will get value as object, it is based on my C# function.

By combining each function together, you can automate a lot of things, automate fill textbox, click button, extract any kind of data, automate, create, delete, file, folder, automate extract data from website and save it to Excel file easily, update data to Excel file, read data from Excel file, control your mouse, your keyboard.....

I try to include all knowledge I have on my program like OCR, Text To Speech, Speech To Text,... My program can be expanded by adding function on JavaScript, then adding function on C# to solve some algorithm.


This is just one function of my software. In my software, I give some techniques to help automation better like record your mouse and keyboard. It helps automate some parts that do not work on software, or control program by speech.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Đinh Công Thắng
Software Developer CSC Viet Nam
Vietnam Vietnam
I worked on Phoenix Software and CSC Viet Nam.
I have knowledge in C#, ASP.NET, Javascript, Jquery, Ajax, SQL Server, MVC 3.

My purpose is to apply what I've learned into real world, create utility so that it can help another person to not repeat their work day by day, of course it's not easy but I'm gonna try and hope the best happen.


Comments and Discussions

QuestionDownload pdf button calling java script is not working Pin
Member 1348467325-Oct-17 2:03
MemberMember 1348467325-Oct-17 2:03 
AnswerRe: Download pdf button calling java script is not working Pin
Đinh Công Thắng6-Nov-17 22:28
MemberĐinh Công Thắng6-Nov-17 22:28 
GeneralHow can i read captcha and write it in text box? Pin
Member 26461544-Oct-17 21:20
MemberMember 26461544-Oct-17 21:20 
GeneralRe: How can i read captcha and write it in text box? Pin
Đinh Công Thắng6-Nov-17 22:27
MemberĐinh Công Thắng6-Nov-17 22:27 
Question not opening Pin
jayesh.sharma18-Sep-17 23:36
Memberjayesh.sharma18-Sep-17 23:36 
QuestionHow to handle alert, popup messages Pin
Member 1297856011-Sep-17 19:21
MemberMember 1297856011-Sep-17 19:21 
GeneralHI Pin
thawpeek18-Apr-17 20:28
Memberthawpeek18-Apr-17 20:28 
QuestionPb and right click Pin
Member 1237957923-Feb-17 21:21
MemberMember 1237957923-Feb-17 21:21 
AnswerRe: Pb and right click Pin
Đinh Công Thắng27-Feb-17 0:25
MemberĐinh Công Thắng27-Feb-17 0:25 
QuestionQuery Regarding Web-Atomation-Master Pin
Member 128875884-Jan-17 23:55
MemberMember 128875884-Jan-17 23:55 
QuestionIssue Regarding Web automation master Pin
Member 128875884-Jan-17 23:58
MemberMember 128875884-Jan-17 23:58 
AnswerRe: Issue Regarding Web automation master Pin
Đinh Công Thắng5-Jan-17 17:43
MemberĐinh Công Thắng5-Jan-17 17:43 
QuestionDownloaded files Pin
Max Lambert27-Oct-16 2:30
MemberMax Lambert27-Oct-16 2:30 
QuestionDownload Application Pin
Marcus196426-Oct-16 2:59
MemberMarcus196426-Oct-16 2:59 
AnswerRe: Download Application Pin
Đinh Công Thắng26-Oct-16 20:29
MemberĐinh Công Thắng26-Oct-16 20:29 
GeneralRe: Download Application Pin
Max Lambert27-Oct-16 2:23
MemberMax Lambert27-Oct-16 2:23 
GeneralRe: Download Application Pin
Đinh Công Thắng27-Oct-16 18:45
MemberĐinh Công Thắng27-Oct-16 18:45 
GeneralRe: Download Application Pin
HBallin25-Dec-16 7:02
MemberHBallin25-Dec-16 7:02 
GeneralRe: Download Application Pin
Đinh Công Thắng3-Jan-17 23:32
MemberĐinh Công Thắng3-Jan-17 23:32 
QuestionNot able to read and write in excel file !! Pin
Member 1252631416-May-16 2:31
MemberMember 1252631416-May-16 2:31 
AnswerRe: Not able to read and write in excel file !! Pin
Đinh Công Thắng14-Oct-16 2:22
MemberĐinh Công Thắng14-Oct-16 2:22 
Questionhow to handle upload file or openfile dialogue box using this tool? Pin
Member 1252631416-May-16 2:27
MemberMember 1252631416-May-16 2:27 
AnswerRe: how to handle upload file or openfile dialogue box using this tool? Pin
Đinh Công Thắng14-Oct-16 2:26
MemberĐinh Công Thắng14-Oct-16 2:26 
QuestionFill input in iFrame Pin
syl#11-Feb-16 7:52
Membersyl#11-Feb-16 7:52 
AnswerRe: Fill input in iFrame Pin
Đinh Công Thắng14-Oct-16 2:23
MemberĐinh Công Thắng14-Oct-16 2:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.