RSelenium使用Docker和Firefox,浏览器无法在Mac上打开

时间:2018-03-14 23:09:02

标签: docker rselenium

我正在尝试将RSelenium与Docker一起使用,一切似乎都在工作,除了我看不到firefox浏览器。我不认为我的代码中有任何东西要求它无头,但必须如此。有没有办法让它像使用RSelenium::startServer()时那样可见?

我是按照本文https://www.raynergobran.com/2017/01/rselenium-mac-update/

中的说明安装了docker的

我去了终端并输入了docker run -d -p 4445:4444 selenium/standalone-firefox,没有问题。

我转到R并运行此代码:

library(RSelenium)
remDr <- remoteDriver(port=4445L, browserName = "firefox")
remDr$open()
remDr$getStatus()
remDr$navigate("https://www.google.com/")
remDr$getCurrentUrl()

最后一个命令的输出符合预期:

[[1]]
[1] "https://www.google.com/"

remDr$open()之后查看输出时,headless设置为false:

[1] "Connecting to remote server"
$`moz:profile`
[1] "/tmp/rust_mozprofile.EQnMfPLPKS4l"

$rotatable
[1] FALSE

$timeouts
$timeouts$implicit
[1] 0

$timeouts$pageLoad
[1] 300000

$timeouts$script
[1] 30000


$pageLoadStrategy
[1] "normal"

$`moz:headless`
[1] FALSE

$`moz:accessibilityChecks`
[1] FALSE

$acceptInsecureCerts
[1] FALSE

$browserVersion
[1] "58.0.2"

$platformVersion
[1] "4.9.60-linuxkit-aufs"

$`moz:processID`
[1] 741

$browserName
[1] "firefox"

$platformName
[1] "linux"

$`moz:webdriverClick`
[1] TRUE

$webdriver.remote.sessionid
[1] "bfa2d87a-67d3-4e5e-838b-e6894a90dd5c"

$id
[1] "bfa2d87a-67d3-4e5e-838b-e6894a90dd5c"

同样,浏览器在我的屏幕上无法查看,但我希望它能够显示。

我正在运行El Capitan 10.11.6。

感谢您的帮助。

2 个答案:

答案 0 :(得分:2)

我认为这个问题似乎与您对浏览器的期望有关。您正在Docker容器中启动浏览器。 docker容器没有UI,浏览器在容器实例内启动,而不是在启动调用的计算机上启动

默认selenium/standalone-firefox不允许查看浏览器。如果您需要查看浏览器,则需要使用selenium/standalone-firefox-debug图像。此映像在容器的端口5900上托管VNC。您将使用下面的

运行它
docker run -d -p 4445:4444 -p 5900:5900 selenium/standalone-firefox-debug

然后,您将执行代码并使用VNC Viewer连接到127.0.0.1:5900。它会要求输入密码,默认为secret

然后您将看到该VNC中的浏览器

VNC View

以上是运行您遇到的代码的结果

答案 1 :(得分:1)

这是一个可能对您有用的Dockerfile https://github.com/jessfraz/dockerfiles/blob/master/chrome/stable/Dockerfile

为方便起见,

在此处内联:

# Run Chrome in a container
#
# docker run -it \
#   --net host \ # may as well YOLO
#   --cpuset-cpus 0 \ # control the cpu
#   --memory 512mb \ # max memory it can use
#   -v /tmp/.X11-unix:/tmp/.X11-unix \ # mount the X11 socket
#   -e DISPLAY=unix$DISPLAY \
#   -v $HOME/Downloads:/home/chrome/Downloads \
#   -v $HOME/.config/google-chrome/:/data \ # if you want to save state
#   --security-opt seccomp=$HOME/chrome.json \
#   --device /dev/snd \ # so we have sound
#   --device /dev/dri \
#   -v /dev/shm:/dev/shm \
#   --name chrome \
#   jess/chrome
#
# You will want the custom seccomp profile:
#   wget https://raw.githubusercontent.com/jfrazelle/dotfiles/master/etc/docker/seccomp/chrome.json -O ~/chrome.json

# Base docker image
FROM debian:sid
LABEL maintainer "Jessie Frazelle <jess@linux.com>"

ADD https://dl.google.com/linux/direct/google-talkplugin_current_amd64.deb /src/google-talkplugin_current_amd64.deb

# Install Chrome
RUN apt-get update && apt-get install -y \
    libcanberra-gtk* \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg \
    hicolor-icon-theme \
    libgl1-mesa-dri \
    libgl1-mesa-glx \
    libpango1.0-0 \
    libpulse0 \
    libv4l-0 \
    fonts-symbola \
    --no-install-recommends \
    && curl -sSL https://dl.google.com/linux/linux_signing_key.pub | apt-key add - \
    && echo "deb [arch=amd64] https://dl.google.com/linux/chrome/deb/ stable main" > /etc/apt/sources.list.d/google.list \
    && apt-get update && apt-get install -y \
    google-chrome-stable \
    --no-install-recommends \
    && dpkg -i '/src/google-talkplugin_current_amd64.deb' \
    && apt-get purge --auto-remove -y curl \
    && rm -rf /var/lib/apt/lists/* \
    && rm -rf /src/*.deb

# Add chrome user
RUN groupadd -r chrome && useradd -r -g chrome -G audio,video chrome \
    && mkdir -p /home/chrome/Downloads && chown -R chrome:chrome /home/chrome

COPY local.conf /etc/fonts/local.conf

# Run Chrome as non privileged user
USER chrome

# Autorun chrome
ENTRYPOINT [ "google-chrome" ]
CMD [ "--user-data-dir=/data" ]

此外,此项目https://github.com/sameersbn/docker-browser-box

内联Dockerfile以方便使用。

FROM sameersbn/ubuntu:14.04.20170123

ENV TOR_VERSION=6.5 \
    TOR_FINGERPRINT=0x4E2C6E8793298290

RUN wget -q -O - "https://dl-ssl.google.com/linux/linux_signing_key.pub" | sudo apt-key add - \
 && echo "deb http://dl.google.com/linux/chrome/deb/ stable main" > /etc/apt/sources.list.d/google-chrome.list \
 && mkdir ~/.gnupg \
 && gpg --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys ${TOR_FINGERPRINT} \
 && gpg --fingerprint ${TOR_FINGERPRINT} | grep -q "Key fingerprint = EF6E 286D DA85 EA2A 4BA7  DE68 4E2C 6E87 9329 8290" \
 && apt-get update \
 && DEBIAN_FRONTEND=noninteractive apt-get install -y xz-utils file locales dbus-x11 pulseaudio dmz-cursor-theme curl \
      fonts-dejavu fonts-liberation hicolor-icon-theme \
      libcanberra-gtk3-0 libcanberra-gtk-module libcanberra-gtk3-module \
      libasound2 libglib2.0 libgtk2.0-0 libdbus-glib-1-2 libxt6 libexif12 \
      libgl1-mesa-glx libgl1-mesa-dri libstdc++6 nvidia-346 \
      gstreamer-1.0 gstreamer1.0-plugins-base gstreamer1.0-plugins-good \
      gstreamer1.0-plugins-bad gstreamer1.0-plugins-ugly gstreamer1.0-libav \
      google-chrome-stable chromium-browser firefox \
 && update-locale LANG=C.UTF-8 LC_MESSAGES=POSIX \
 && mkdir -p /usr/lib/tor-browser \
 && wget -O /tmp/tor-browser-linux64-${TOR_VERSION}_en-US.tar.xz  https://www.torproject.org/dist/torbrowser/${TOR_VERSION}/tor-browser-linux64-${TOR_VERSION}_en-US.tar.xz \
 && wget -O /tmp/tor-browser-linux64-${TOR_VERSION}_en-US.tar.xz.asc https://www.torproject.org/dist/torbrowser/${TOR_VERSION}/tor-browser-linux64-${TOR_VERSION}_en-US.tar.xz.asc \
 && gpg /tmp/tor-browser-linux64-${TOR_VERSION}_en-US.tar.xz.asc \
 && tar -Jvxf /tmp/tor-browser-linux64-${TOR_VERSION}_en-US.tar.xz --strip=1 -C /usr/lib/tor-browser \
 && ln -sf /usr/lib/tor-browser/Browser/start-tor-browser /usr/bin/tor-browser \
 && rm -rf /tmp/tor-browser-linux64-${TOR_VERSION}_en-US.tar.xz \
 && rm -rf ~/.gnupg \
 && rm -rf /var/lib/apt/lists/*

COPY scripts/ /var/cache/browser-box/
COPY entrypoint.sh /sbin/entrypoint.sh
COPY confs/local.conf /etc/fonts/local.conf
RUN chmod 755 /sbin/entrypoint.sh

ENTRYPOINT ["/sbin/entrypoint.sh"]