I wrote a quick and dirty facebook scraper using selenium webdriver and it worked well for a while but I would occasionally get an EADDRINUSE error. Now I get it all the time and the script barely runs. Here is the error:
throw error;
^
Error: EADDRINUSE connect EADDRINUSE 127.0.0.1:56642
at ClientRequest.<anonymous> (C:\Users\ConquerJS\node_modules\selenium-webdriver\http\index.js:238:15)
at emitOne (events.js:96:13)
at ClientRequest.emit (events.js:188:7)
at Socket.socketErrorListener (_http_client.js:308:9)
at emitOne (events.js:96:13)
at Socket.emit (events.js:188:7)
at emitErrorNT (net.js:1272:8)
at _combinedTickCallback (internal/process/next_tick.js:74:11)
And here is my code. It might make you want to vomit but I was asked to do this and did it reluctantly as fast as I could, and I'm a beginner at coding :
var webdriver = require('selenium-webdriver'),
chrome = require('selenium-webdriver/chrome'),
By = webdriver.By,
until = webdriver.until;
var o = new chrome.Options();
o.addArguments("--disable-notifications");
var driver = new webdriver.Builder().withCapabilities(webdriver.Capabilities.chrome()).setChromeOptions(o).build()
driver.manage().window().maximize();
var find = function (el) {
driver.wait(until.elementLocated(By.css(el)), 10000, 'Could not locate element');
driver.sleep(700);
return driver.findElement(By.css(el));
};
var findAll = function (el) {
driver.wait(until.elementLocated(By.css(el)), 10000, 'Could not locate element');
driver.sleep(700);
return driver.findElements(By.css(el));
};
var styleSheet = `
<style>
body{
font-family: "raleway";
font-weight: 100;
}
h1{
margin-top: 60px;
font-weight: bold;
}
.num{
color:red;
}
.gender{
color: orange;
}
li{
display: inline-block;
max-width: 100px;
max-height: 120px;
overflow: hidden;
}
header {
width: 100%;
background: #3d3da7;
}
.headerwrap {
height: 100px;
color: white;
line-height: 100px;
max-width: 80%;
margin: 0 auto;
}
</style>
<header><div class="headerwrap">FBPromoter9000</div></header>
`
listAllElements = function (el, loc){
driver.wait(until.elementLocated(By.css(el)), 10000, 'Could not locate element');
var findElements = driver.findElements(By.css(el));
findElements.then(function (options) {
var htmlFormatting = `
<h1> - There are a total of <span class="num">` + options.length + ` girls </span> from <span class="num">` + loc + `</span> Currently listed as living in New York</h1>
`
console.log(htmlFormatting);
options.map(function (elem) {
var picSrc = elem.getAttribute("src");
picSrc.then(function(url){
var picIndex = options.indexOf(elem);
driver.findElements(By.css('._5d-5')).then(function(peopleNames){
peopleNames.map(function(personName){
if(peopleNames.indexOf(personName) === picIndex){
personName.getText().then(function(t){
driver.findElements(By.css('._2ial')).then(function(profileLinks){
profileLinks.map(function(currentProfile){
if(profileLinks.indexOf(currentProfile) === picIndex){
currentProfile.getAttribute("href").then(function(linkToProfile){
console.log(" <li><a href="+"'"+linkToProfile+"'"+">"+"<img src="+"'"+url+"'" + "/></a><br/>" + "<a href="+"'"+linkToProfile+"'"+">"+t+"</a></li>");
});
};
});
});
});
};
});
});
});
});
});
};
var scrollToBottom = function () {
var el = "#browse_end_of_results_footer";
driver.findElement(By.css(el)).catch(function(err){
if (err){
driver.executeScript("window.scrollBy(0, 1000)", "");
scrollToBottom();
}else{
console.log("WASSSSSUUUPP");
}
});
}
driver.get(searchqueryurl);
find('#email').sendKeys(useremail);
find('#pass').sendKeys(userpass);
find('#loginbutton').click();
var searchForPeople = function(loc){
driver.get(searchquery);
scrollToBottom();
driver.sleep(1000);
find("#browse_end_of_results_footer").getText().then(function(txt){
listAllElements('._3u1 ._2ial img', loc);
});
};
console.log(styleSheet);
searchForPeople('germany');
searchForPeople('france');
searchForPeople('Sweden');
searchForPeople('Norway');
searchForPeople('Denmark');
searchForPeople('finland');
searchForPeople('uk');
searchForPeople('australia');
searchForPeople('ireland');
searchForPeople('switzerland');
searchForPeople('canada');
searchForPeople('holland');
searchForPeople('italy');
// searchForPeople('austria');
searchForPeople('belgium');
searchForPeople('mexico');
searchForPeople('spain');
searchForPeople('portugal');
searchForPeople('greece');
searchForPeople('poland');
searchForPeople('ukraine');
searchForPeople('russia');
searchForPeople('brazil');
searchForPeople('colombia');
searchForPeople('turkey');
The reason I'm console logging html is for the output to come out as a formatted webpage by running the following in the terminal:
node testscript.js > searchresults.html