0

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
codemon
  • 1,456
  • 1
  • 14
  • 26
  • 2
    What did you find when you searched that term? –  Jan 16 '17 at 01:36
  • 4
    EADDRINUSE = E ADDR IN USE (port number in use) – Andrew Li Jan 16 '17 at 01:37
  • read this question http://stackoverflow.com/questions/4075287/node-express-eaddrinuse-address-already-in-use-kill-server – parik Jan 16 '17 at 01:38
  • I found the stackoverflow question linked to by Andrew Li but I don't understand what my code is doing that causes this error. And in that question everybody seems to already know what the error means and they just discuss various random methods to solve it. I would like to know exactly what this error means and why my script is causing it. – codemon Jan 16 '17 at 01:42
  • 1
    Rup: @jfriend00 has answered nearly 9,000 questions here. You've answered 2. What was that you were saying about making contributions? If you think you're entitled to someone's assistance, you're mistaken. –  Jan 16 '17 at 02:15
  • @squint I defintitely saw his answer count and he could be the founder of SO for all I care it does not entitle him to treat beginners like that and making assumptions that are simply not true. His comment literally contributed nothing and only belittled. Notice how no one else adopted that tone except him. – codemon Jan 16 '17 at 02:17
  • 1
    So if you found things in your research you should reference them in the question with an explanation of why they didn't work instead of blindly asking what that error means. Surely you found out what it at least means and can ask a more intelligent question based on this supposed research. Some reading in the [help] will also be beneficial research for the future – charlietfl Jan 16 '17 at 02:36
  • I'll withdraw all my comments and leave you to the rest of the community. Good luck. – jfriend00 Jan 16 '17 at 02:48
  • @charlieftl I can edit my question to your criteria, just saying that initially without the rudeness exibited by jfriend is helpful at least. – codemon Jan 16 '17 at 04:22

2 Answers2

3

The accepted answer by Nicholas Smith may not be correct. Selenium polls ChromeDriver which can exhaust the ephemeral ports in Windows. See this GitHub issue: https://github.com/seleniumhq/selenium/issues/2888

Drew Wiens
  • 124
  • 1
  • 8
  • Nice! I never ran against that issue again (though I am now familiar with EADDRINUSE errors) but looking at the github issue it seems that this was the problem back then. Thanks. – codemon Oct 09 '19 at 17:53
  • In fact this is a generally valid question and answer when it's not about listening sockets. It is very rare that someone gets EADDRINUSE when _connecting_. And indeed, it's the sign that the given address have ran out all the TCP/UDP sockets available. – grin Sep 07 '22 at 08:03
2

The error EADDRINUSE means you have multiple instances of your server running or multiple node.js servers running on your desired port.

Nicholas Smith
  • 674
  • 1
  • 13
  • 27
  • 1
    I accept your answer but to be honest I don't understand how selenium webdriver or Node works well enough to understand why this error happens in the middle of a test running. Meaning the browser spins up and the test runs then suddenly EADDRINUSE. Eventually the error stopped, but now idea what exactly caused it. – codemon Jan 24 '17 at 18:30
  • is there a way to avoid it or ensuring that other servers are terminated? – Priyesh May 20 '20 at 10:40