我使用selenium webdriver编写了一个快速而又脏的facebook刮刀,它运行了一段时间,但偶尔会出现EADDRINUSE错误。现在我总是得到它,脚本几乎没有运行。这是错误:
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)
这是我的代码。它可能会让你想要呕吐但是我被要求这样做并且尽可能快地不情愿地做到了,而且我是编码的初学者:
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');
我控制台记录html的原因是输出通过在终端中运行以下内容作为格式化网页:
node testscript.js > searchresults.html
答案 0 :(得分:2)
错误 EADDRINUSE 表示您运行的服务器有多个实例,或者您所需的端口上运行了多个node.js服务器。
答案 1 :(得分:0)
尼古拉斯·史密斯(Nicholas Smith)接受的答案可能不正确。 Selenium会轮询ChromeDriver,它可能会耗尽Windows中的临时端口。看到这个GitHub问题:https://github.com/seleniumhq/selenium/issues/2888