Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I interact with a third-party web page using JavaScript?

My company has a JavaScript-heavy (Backbone-based) web site living at an external domain which I have no control over. I'd like to automate some tasks. Basically I want to click on some things, fill out some form fields, and submit a button. However, I also want the ability to do logic tests and enter different values in fields based on various states of things in the page.

I know I can probably use Selenium for this, but are there any alternatives? Maybe a pure JavaScript solution?

For instance, if I could do this, it would be great:

$('#someButton').click();

if (parseInt($('#someDiv').text()) > x) {
    $('#someField').val(123);
    someCounter++;
}

$('#submitButton').click();

Even if I could inject custom JavaScript into the web page...that could work too.

like image 313
Chad Johnson Avatar asked Oct 30 '25 08:10

Chad Johnson


1 Answers

You can do this with selenium using pure Js. basically you'll need the WebDriverJs.

Here some code example.

var webdriver = require('selenium-webdriver');

var driver = new webdriver.Builder().
   withCapabilities(webdriver.Capabilities.chrome()).
   build();

driver.get('http://www.google.com');
driver.findElement(webdriver.By.name('q')).sendKeys('webdriver');
driver.findElement(webdriver.By.name('btnG')).click();
driver.wait(function() {
 return driver.getTitle().then(function(title) {
   return title === 'webdriver - Google Search';
 });
}, 1000);

driver.quit();

More information here https://code.google.com/p/selenium/wiki/WebDriverJs

like image 110
rcarvalho Avatar answered Oct 31 '25 22:10

rcarvalho



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!