Creating a bot in JS

2

I'm developing a bot in javascript where it will insert a value into a field of a website and click 'send'.

window.onload = function() {
    window.open('http://www.google.com', '_self');
    document.getElementById('gbqfq').value = 'javascript';
    document.getElementById('gbqfb').click();
}

But I found a code similar to that on the internet, it opens the page but does not enter the value in the search field and clicks 'go'. Would anyone have an idea similar to this 'no solution'?

    
asked by anonymous 14.04.2014 / 21:54

4 answers

4

Through javascript running within the context of a page (and limited by the browser sandbox) it is not possible to manipulate documents from another domain, either in a new window or in an iframe. This is for security reasons.

If you want a visual answer to this: see the browser window opening and the commands being executed (maybe to pass the control to the user after it reaches some given state, such as after clicking search), it basically has two solutions:

  • An extension to the browser:

    Extensions can inject code into a specific page (like inserting an extra button on the google page), or add a direct button on the browser bar, or shortcut keys. Any of these can be trigger to start action: manipulating the DOM of the page.

    The downside is that you will have to do an extension for each browser you want to support, possibly using completely different APIs.

  • Automation Library:

    Use an external program that will launch a new browser process and will "control" it. This is supported by all major browsers and is relatively simple to program. Two good libraries: Selenium and < strong> Watir .

  • Finally, you may not want a visible browser or iteration with the user. Want to do everything dark and just get the result later (an array of search results titles, for example). For this you need a headless browser.

  • PhantomJS :

    As already mentioned in other answers. It is an interface to WebKit, but without creating a window or visual of any kind. You're in control of everything.

  • Deploy your HTTP Client:

    Here you can use any language you want. Simply open a TCP socket and communicate with the site server using the HTTP protocol. The big problem here is that the page javascript will not run and will have to render the HTML manually, you will be very limited. On the other hand it is the fastest and most efficient way to get data. You can make a single request for http://www.google.com/search?q=teste for example.

  • 15.04.2014 / 15:21
    1

    The browser will NOT allow this because of the sandbox - it is a security restriction of any browser.

    Using link to be able to manipulate native pages is an alternative.

        
    15.04.2014 / 06:29
    1

    You can use PhantomJs to simulate the browser using scripts made using javascript.

    What you can do with PhantomJs:

    • Navigation automation (this is probably what you want)

    • testing websites

    • Take screenshots

    • convert page to PDF

    The project uses the webkit code, and allows you to do various things using javascript scripts. No browser is required on the machine where PhantomJs is installed.

    Idea to make it work in the Browser

    Although you can make the robot using javascript as a language, this robot will not run from a web page, but rather from a desktop executable program.

    However, it would be possible to create a web-service that calls PhantomJs, making it possible through the browser, which would call the web-service.

    You can do the web service using C # for example, which supports PhantonJs through a library.

        
    14.04.2014 / 22:58
    1

    I recently had the opportunity to work with this library / solution: link that uses Selenium drivers, supporting automation through Firefox, Chrome, Internet Explorer, Opera, htmlUnit, PhantomJS and others.

    The language used for automating the tests is Groovy (based on java), using a navigation library that supports selectors similar to the known JQuery selectors.

    I recommend beginning here as an example: link

        
    15.04.2014 / 17:11