node.js - Selenium Webdriver JS 并行抓取 [nodejs]

标签 node.js selenium selenium-webdriver phantomjs

我正在尝试创建一个 Phantom Webdriver 池 [使用 webdriverjs],例如

var driver = new Webdriver.Builder().withCapabilities(Webdriver.Capabilities.phantomjs()).build();

一旦池被填充[我看到产生了n个虚拟进程],我尝试对不同的url执行driver.get[使用池中的不同驱动程序],希望它们能够并行工作[因为 driver.get 是异步的]。

但我总是看到它们是按顺序完成的。 我们不能通过不同的 Web 驱动程序实例并行加载不同的 url 吗? 如果不可能以这种方式我还能如何解决这个问题?

我的问题的非常基本的实现如下所示

var Webdriver = require('selenium-webdriver'),

function getInstance() {
   return new Webdriver.Builder().withCapabilities(Webdriver.Capabilities.phantomjs()).build();
}

var pool = [];
for (var i = 0; i < 3; i++) {
  pool.push(getInstance());
}
pool[0].get("http://mashable.com/2014/01/14/outdated-web-features/").then(function () { 
  console.log(0);
});

pool[1].get("http://google.com").then(function () { 
  console.log(1);
});

pool[2].get("http://techcrunch.com").then(function () { 
  console.log(2);
});

PS:已经发了here

更新: 我尝试使用 Selenium 网格进行以下设置;正如前面提到的,它可以并行运行测试

中心:

java -jar selenium/selenium-server-standale-2.39.0.jar -hosost 127.0.0.1 -port 4444 -role hub -nodeTimeout 600

幻影:

phantomjs --webdriver=7777 --webdriver-selium-grid-hub=http://127.0.0.1:4444 --debug=true
phantomjs --webdriver=7877 --webdriver-selium-grid-hub=http://127.0.0.1:4444 --debug=true
phantomjs --webdriver=6777 --webdriver-selium-grid-hub=http://127.0.0.1:4444 --debug=true

我仍然看到 get 命令排队并按顺序执行,而不是并行执行。 [但在 3 个实例中正确分布]

我是不是还漏掉了什么?

为什么文档中提到“通过在多台机器上分布测试来扩展(并行执行)”?

什么是集线器并行?我越来越不懂了

最佳答案

我想我遇到了这个问题..

基本上https://code.google.com/p/selenium/source/browse/javascript/node/selenium-webdriver/executors.js#39是同步且阻塞的操作[至少是get]。 每当发出 get 命令时, Node 的主线程 get 就会卡在那里。不再执行代码。

关于node.js - Selenium Webdriver JS 并行抓取 [nodejs],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21934794/

相关文章:

ruby - 你如何使用页面对象自动确认 ruby​​ 中的模态?

javascript - 一般 Node.js/javascript 问题和事件循环

python - 如何使用 selenium 和 Python 调用 send_keys()

selenium - org.openqa.selenium.SessionNotCreatedException : Unable to find a matching set of capabilities while initiating Firefox v37 through Selenium v3. 11.0

python - 如何格式化 Beautiful Soup 和 Selenium 的输出?

java - 无法实例化类 packageName.pageName

java - Selenium 网格打开没有 URL 的空白页面,而只有 chrome 运行测试

javascript - 如何在我的 Node.js 脚本中从我的 Gmail 帐户发送电子邮件?

node.js - 带 Snap 的 Nodejs

node.js - npm 错误! cb() 从未调用过