Creating a JS bot

Asked

Viewed 6,797 times

2

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

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

However I found a code similar to this on the internet, it opens the page but does not insert the value in the search field and click 'go'. Someone would have an idea similar to that 'no solution''?

  • 6

    You can only do this in the form of a browser or bookmarklet extension, or if you find a way to inject scripts into the desired site (some vulnerability that allows XSS).

  • In some other programming it is possible to make this access and send information simulating a user typing in the field?

  • A bookmarklet does not solve?

  • @ralfting any language that has TCP/IP communication theoretically allows, with greater or lesser complexity. You’d better explain what you’re trying to do, so you can point out the best way.

  • guy maybe you can run by the browser console. google Chrome allows

4 answers

4


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


If you want a visual response to this: see the browser window opening and the commands being executed (maybe to pass control to the user after they arrive at some given state, like after clicking on search), there are basically two solutions:

  1. A browser extension:

    Extensions can inject code into a specific page (such as inserting an extra button into the google page), or add a direct button in the browser bar, or hotkeys. Any of these can be trigger to start the action: manipulate the page’s DOM.

    The downside is that you will have to make an extension for every browser you want to support, possibly using completely different Apis.

  2. 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 Watir.


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

  1. Phantomjs:

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

  2. Deploy your HTTP Client:

    Here you can use any language you want. Just open a TCP socket and communicate with the site server using the HTTP protocol. The big problem here is that the javascript of the page will not run and will have to process 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 to http://www.google.com/search?q=teste for example.

1

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

What you can do with Phantomjs:

  • navigation automation (probably that’s what you want)

  • website testing

  • take screenshots

  • convert page to PDF

The project uses Webkit code, and allows doing various things using javascript scripts. No browser required to be installed on the machine on which the 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 from a desktop executable program.

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

You can do the web service using C# for example, which has Phantonjs support through a library.

1

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

Use the http://phantomjs.org to be able to manipulate native pages is an alternative.

1

I recently had the opportunity to work with this library/solution: http://www.gebish.org/ using Selenium drivers, supporting automation via Firefox, Chrome, Internet Explorer, Opera, htmlUnit, Phantomjs and others.

The language used for test automation is Groovy (java-based), using a navigation library that supports selectors similar to the known Jquery selectors.

I recommend starting here as an example: https://github.com/geb/geb-example-gradle

Browser other questions tagged

You are not signed in. Login or sign up in order to post.