简体   繁体   English

从R或python运行yaml文件进行并行selenium测试

[英]Run yaml file for parallel selenium test from R or python

I have a simple yaml file: 我有一个简单的yaml文件:

seleniumhub:
    image: selenium/hub
    ports:
      - 4444:4444

firefoxnode:
    image: selenium/node-firefox-debug
    ports:
      - 4577
    links:
      - seleniumhub:hub

chromenode:
    image: selenium/node-chrome-debug
    ports:
      - 4578
    links:
      - seleniumhub:hub

that I have executed in docker: 我在docker中执行过:

docker-compose up -d

I have one hub and two nodes running. 我有一个集线器和两个节点在运行。

Now I would like to run two very simple selenium commands in parallel (written in RSelenium): 现在我想并行运行两个非常简单的selenium命令(用RSelenium编写):

remDr$open()
remDr$navigate("http://www.r-project.org")
remDr$screenshot(display = TRUE)

I would like to know how can I run above selenium commands in Python or R, in parallel. 我想知道如何在Python或R中并行运行selenium命令。 I tried several ways but none works. 我尝试了几种方法但没有效果。 For example in R: 例如在R中:

library(RSelenium)
remDr <- remoteDriver(remoteServerAddr = "192.168.99.100", port = 4444L)
remDr$open()
remDr$navigate("http://www.r-project.org")
remDr$screenshot(display = TRUE)

doesn't do anything. 什么都不做 I have also tried to run two remoteDrivers, but that doesn't help ether: 我也试过运行两个remoteDrivers,但这对以太没有帮助:

remDr <- remoteDriver(remoteServerAddr = "192.168.99.100", port = 4577L)
remDr$open()
remDr$navigate("http://www.r-project.org")
remDr$screenshot(display = TRUE)

This is duplicate of 这是重复的

Run RSelenium in parallel 并行运行RSelenium

You can use code in above answer to do parallel execution 您可以使用上面的代码中的代码来执行并行执行

library(RSelenium)
library(rvest)
library(magrittr)
library(foreach)
library(doParallel)

URLsPar <- c("http://www.bbc.com/", "http://www.cnn.com", "http://www.google.com",
             "http://www.yahoo.com", "http://www.twitter.com")
appHTML <- c()

(cl <- (detectCores() - 1) %>%  makeCluster) %>% registerDoParallel
# open a remoteDriver for each node on the cluster
clusterEvalQ(cl, {
  library(RSelenium)
  remDr <- remoteDriver$new(remoteServerAddr = ip, port = port)
  remDr$open()
})
myTitles <- c()
ws <- foreach(x = 1:length(URLsPar), .packages = c("rvest", "magrittr", "RSelenium"))  %dopar%  {
  remDr$navigate(URLsPar[x])
  remDr$getTitle()[[1]]
}

# close browser on each node
clusterEvalQ(cl, {
  remDr$close()
})

stopImplicitCluster()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM