Docker Selenium(1) 搭建服務及chrome 使用 firefox

Docker Selenium能讓Selenium在Docker中運行,可加速建置時間及獨立出各瀏覽器的版本
,保持了一定的隔離性,是非常好的測試環境。

docker-selenium 官方文檔

https://www.lfhacks.com/tech/selenium-docker

https://hub.docker.com/r/selenium/standalone-chrome/dockerfile

鏡像介紹

selenium/hub: Grid Hub,相當於一個空白的Seleniun Server,
selenium/node-chrome: Chrome節點,需加入Grid Hub才能使用。
selenium/node-firefox: Firefox節點,需加入Grid Hub才能使用。
selenium/node-chrome-debug: Chrome節點,同時包含VNC Server,需加入Grid Hub才能使用。
selenium/node-firefox-debug: Firefox節點,同時包含VNC Server,需加入Grid Hub才能使用

selenium/standalone-chrome: 包含Chrome的Seleniun Server,可直接調用。
selenium/standalone-firefox: 包含Chrome的Seleniun Server,可直接調用。
selenium/standalone-chrome-debug: 包含Chrome的Seleniun Server,同時包含VNC Server,可直接調用。
selenium/standalone-firefox-debug: 包含Chrome的Seleniun Server,同時包含VNC Server,可直接調用。

使用

docker conpose方法

selenium 官方提供了docker compose方法,直接使用就可以輕鬆搭建了。

官方連結

  1. docker-compose.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# To execute this docker-compose yml file use docker-compose -f <file_name> up
# Add the "-d" flag at the end for deattached execution
version: "3"
services:
  selenium-hub:
    image: selenium/hub:3.12.0-boron
    container_name: selenium-hub
    ports:
      - "4448:4444"
  chrome:
    image: selenium/node-chrome:3.12.0-boron
    depends_on:
      - selenium-hub
    environment:
      - HUB_HOST=selenium-hub
      - HUB_PORT=4444
  firefox:
    image: selenium/node-firefox:3.12.0-boron
    depends_on:
      - selenium-hub
    environment:
      - HUB_HOST=selenium-hub
      - HUB_PORT=4444
  1. docker-compose up --build

 

  1. 搭建成功

docker-compose.yaml中我指定公開的是4448端口,打開頁面看到可以成功訪問,能調用的瀏覽器有Chrome及Firefox.

 

Python 調用

調用方法與本地搭建selenium server相同,以下以chrome為例。

  1. 創建一個Chrome對象
1
2
3
4
5
6
7
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

browser = webdriver.Remote(
    command_executor='http://192.168.8.22:4448/wd/hub',
    desired_capabilities=DesiredCapabilities.CHROME,
)

如果成功調用,在slenium頁面也會看到一個chrome正在被使用。

 

  1. 進入Google首頁,試著打印網頁標題
1
2
3
browser.get('http://www.google.com')
print(browser.title)
# Google
  1. 測試截取當前頁面
1
browser.save_screenshot("chrome.png")

 

  1. 釋放瀏覽器
1
browser.quit()

結束調用,slenium頁面上的chrome已經變回彩色的。

 

另外一種模式 : Debug Mode

Docker Selenium 還有一種 Debug Mode,主要提供了有畫面的selenium,能直接看到瀏覽器在做什麼,Debug Mode使用了VNC服務器並佔用了5900端口,但要注意的是每個端口只能運行一個節點(Chrome、Firefox等),如果希望包含兩個節點或更多,那麼就必須公開不同的端口,如5900:5900、5901:5900…

Docker Compose 方法:

  1. docker-compose up --build
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# To execute this docker-compose yml file use docker-compose -f <file_name> up
# Add the "-d" flag at the end for deattached execution
version: '2'
services:
  firefox:
    image: selenium/node-firefox-debug:3.12.0-boron
    volumes:
      - /dev/shm:/dev/shm
    depends_on:
      - hub
    environment:
      HUB_HOST: hub
    ports:
      - "5900:5900"

  chrome:
    image: selenium/node-chrome-debug:3.12.0-boron
    volumes:
      - /dev/shm:/dev/shm
    depends_on:
      - hub
    environment:
      HUB_HOST: hub
    ports:
      - "5901:5900"

  hub:
    image: selenium/hub:3.12.0-boron
    ports:
      - "4450:4444"
  1. 訪問selenium頁面

這個頁面顯示了Selenium hub的狀態,如果想看到VNC的情況,需要進入Docker 容器才看的到。

 

用VNC 看瀏覽器在做什麼

  1. 安裝VNC軟體

推薦工具 免費VNC : ultravnc

ultravnc下載地址

  1. 打開UltraVNC Viewer

 

下載並安裝完成後,連進Docker內的VNC Server

  1. 點擊連接

 

  1. 輸入密碼

 

默認密碼 : secret

預設是有密碼的,若不希望輸入密碼可在容器變量中加入VNC_NO_PASSWORD: 1

  1. 連接成功

看到這個畫面代表成功連進了VNC

 

Python 調用

  1. 創建一個腳本,測試selnium運行的情況。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.common.keys import Keys

browser = webdriver.Remote(
    command_executor='http://192.168.8.22:4450/wd/hub',
    desired_capabilities=DesiredCapabilities.CHROME,
)

browser.get("http://www.google.com")
browser.find_element_by_id("lst-ib").send_keys("python")
browser.find_element_by_name("btnK").send_keys(Keys.ENTER)
titles = browser.find_elements_by_xpath(".//h3[@class='r']//a")
print([i.text for i in titles])
browser.quit()
  1. 有畫面的selenium出現了

 

小結

docker selenium透過Docker建置的關係,構建非常方便快速,這邊主要介紹了兩種模式創建容器,一種是可以看見瀏覽器的,一種是默默執行的,測試運行速度也非常的快,感覺起來似乎比實體主機還要順上很多,但以長期的測試情況,就不知道會不會有其他問題了。

 

https://maliaotw.github.io/2018/06/08/Docker%20Selenium(1)%20%E6%90%AD%E5%BB%BA/

https://blog.csdn.net/weixin_39198406/article/details/85085505

https://www.jianshu.com/u/a70a89d3258f

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值