Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在不使节点崩溃的情况下停止webdriver_Javascript_Node.js_Selenium_Selenium Webdriver_Webdriver - Fatal编程技术网

Javascript 如何在不使节点崩溃的情况下停止webdriver

Javascript 如何在不使节点崩溃的情况下停止webdriver,javascript,node.js,selenium,selenium-webdriver,webdriver,Javascript,Node.js,Selenium,Selenium Webdriver,Webdriver,是否可以在不停止节点的情况下停止selenium webdriver 我有以下问题: 我尝试创建一个API工具,在获取get请求时实现一些web自动化。因此,我基本上是通过Express在get请求上运行SeleniumWebDriver。我希望工具检查不同的元素,当它以某种方式失败时,我希望它停止selenium,而不是节点实例(因为可以发送新的get请求) 这是我目前的代码: “严格使用”; const webdriver=require('selenium-webdriver'), Exp

是否可以在不停止节点的情况下停止selenium webdriver

我有以下问题: 我尝试创建一个API工具,在获取get请求时实现一些web自动化。因此,我基本上是通过Express在get请求上运行SeleniumWebDriver。我希望工具检查不同的元素,当它以某种方式失败时,我希望它停止selenium,而不是节点实例(因为可以发送新的get请求)

这是我目前的代码:

“严格使用”;
const webdriver=require('selenium-webdriver'),
Express=require('Express'),
By=webdriver.By,
until=webdriver.until,
Keys=webdriver.Key,
app=new Express();
app.get(“/automation)”,(请求、回复)=>{
启动(res);
});
功能启动(res){
const driver=new webdriver.Builder().forBrowser('chrome').build();
司机,上车https://www.google.com/');
//#--foo--#
让errMessage={pos:“FOO”,message:“Ooops友好的机器人有一些麻烦!”}
检查步骤(“foo”,errMessage);
驱动程序findElement(By.id(“foo”))
.sendKeys(“fooz”);
//#---酒吧--#
errMessage={pos:“BAR”,message:“Ooops友好型机器人有一些问题!”}
检查步骤(“#条”,错误消息);
驱动程序findElement(按.id(“bar”))
.sendKeys(“baz”);
//等等…
功能检查步骤(选择器、错误消息){
driver.findElement(By.css(选择器))
.然后(()=>{
log(`${selector}=>found`);
})
.catch(错误=>{
log(`Error:${err}`);
res.send(errMessage);
driver.quit();
});
}
}
应用程序获取(“*”,(请求,请求)=>{
res.send(“你好世界”);
});
//启动服务器
const port=process.env.port | 3000;
const env=process.env.NODE|u env||'production';
app.listen(端口,错误=>{
if(err){return console.error(err);}
console.info(`Server running onhttp://localhost:${port}[${env}]`);
});
它实际上工作到目前为止,当selenium没有找到元素时,API的响应是正确的。在Selenium中,我回来了:

{
  "pos": "FOO",
  "message": "Ooops friendly robot has some troubles!"
}
到目前为止一切都很好。 但不幸的是,停止selenium也会停止节点的运行

我得到的错误如下:

抛出错误;
^
WebDriverError:没有这样的会话
(驾驶员信息:chromedriver=2.30.477690(c53f4ad87510ee97b5c3425a14c0e79780cdf262),平台=Ma
c OS X 10.12.5 x86_64)
在WebDriverError
请帮忙,谢谢


ps:我没有使用WebDrivero,正如您所看到的,我使用的是这个软件包:

好的,我已经开始工作了。这是一个有点困难的解决方案,但它是有效的:

使用子进程 基本上,每次应用程序获得一个
get
请求以
/自动执行
,它现在都会在运行selenium脚本的节点中创建一个子进程(子进程有点像使用另一个线程。下面是一个非常好的例子):

index.js
“严格使用”;
const Express=require('Express');
const{spawn}=require('child_进程');
const data=require('./data.json');
const app=new Express();
app.get(“/automation)”,(请求、回复)=>{
const child=spawn(
process.execPath,
[`${uu dirname}/test.js`,JSON.stringify(数据)],
{stdio:['inherit','inherit','inherit','pipe']}
);
child.stdio[3]。on('data',data=>{
const response=JSON.parse(data.toString());
res.send(响应);
控制台日志(响应);
child.kill();
});
});
应用程序获取(“*”,(请求,请求)=>{
res.send(“你好世界”);
});
const port=process.env.port | 3000;
const env=process.env.NODE|u env||'production';
app.listen(端口,错误=>{
if(err){return console.error(err);}
console.info(`Server running onhttp://localhost:${port}[${env}]`);
});
test.js
“严格使用”;
//与参数3挂钩,即来自父级的“管道”
const Net=require('Net'),
管道=新的网络套接字({fd:3});
const data=JSON.parse(process.argv[2]);
const webdriver=require('selenium-webdriver'),
By=webdriver.By,
until=webdriver.until,
Keys=webdriver.Key;
函数start(){
const driver=new webdriver.Builder().forBrowser('chrome').build();
司机,上车https://www.google.com/');
//#--foo--#
让errMessage={pos:“lst ib”,消息:“Ooops友好型机器人有一些麻烦!”
检查步骤(“第一ib”)
.sendKeys(“fooz”);
司机,上车https://www.facebook.com/');
司机,上车https://www.google.com/');
司机,上车https://www.facebook.com/');
//#---酒吧--#
errMessage={pos:“BAR”,message:“Ooops友好型机器人有一些问题!”}
检查步骤(“#条”)
.sendKeys(“baz”);
功能检查步骤(选择器){
driver.findElement(By.css(选择器))
.然后(()=>{
log(`${selector}=>found`);
})
.catch(错误=>{
log(`${selector}=>找不到`);
发布(错误消息);
driver.quit();
});
}
}
功能发布(消息){
pipe.write(JSON.stringify(message));
}
start();
它的工作原理就像一个符咒:在每个请求上打开一个新的子进程,并在向客户端发送消息的同时杀死该子进程。这样,您就可以轻松地同时拥有多个selenium实例

不客气

ps:如果你讨厌所有这些来自Selenium的asyncron产品,这似乎是一个不错的选择。它基本上将selenium代码包装为syncon而不是asyncron。这样我就可以使用
try{}catch{}
driver.quit()对于后面的代码没有任何错误。(但这也有一个缺点:它实际上会阻塞其他nodejs代码。)