{"id":20619736,"url":"https://github.com/twtrubiks/docker-selenium-tutorial","last_synced_at":"2025-04-15T12:02:09.429Z","repository":{"id":84518941,"uuid":"585105268","full_name":"twtrubiks/docker-selenium-tutorial","owner":"twtrubiks","description":"docker-selenium-tutorial use pyhton","archived":false,"fork":false,"pushed_at":"2023-01-14T04:05:04.000Z","size":5,"stargazers_count":8,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-28T19:53:43.908Z","etag":null,"topics":["docker","python3","selenium"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/twtrubiks.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-01-04T10:31:06.000Z","updated_at":"2023-09-22T17:57:44.000Z","dependencies_parsed_at":"2023-03-02T04:30:27.993Z","dependency_job_id":null,"html_url":"https://github.com/twtrubiks/docker-selenium-tutorial","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/twtrubiks%2Fdocker-selenium-tutorial","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/twtrubiks%2Fdocker-selenium-tutorial/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/twtrubiks%2Fdocker-selenium-tutorial/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/twtrubiks%2Fdocker-selenium-tutorial/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/twtrubiks","download_url":"https://codeload.github.com/twtrubiks/docker-selenium-tutorial/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249067779,"owners_count":21207395,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["docker","python3","selenium"],"created_at":"2024-11-16T12:12:23.502Z","updated_at":"2025-04-15T12:02:09.423Z","avatar_url":"https://github.com/twtrubiks.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# docker-selenium-tutorial\n\n[Youtube Tutorial - docker-selenium-tutorial 教學 Python](https://youtu.be/pXOFmK0eVDk)\n\n## 說明\n\n以前在使用 selenium 時, 都需要先抓對應的 driver, 然後再開始使用,\n\n如果電腦比較差, 跑起來又會比較慢, 而且也蠻吃電腦資源的, 所以, 今天\n\n要來介紹 server 版本的 selenium 概念, 就是 docker-selenium, repo 如下\n\n[https://github.com/SeleniumHQ/docker-selenium](https://github.com/SeleniumHQ/docker-selenium)\n\n這樣子就可以把它執行在 server, 也不吃自己本機電腦的資源,\n\n重點是大家還可以一起使用:thumbsup:\n\n## 介紹\n\n### Standalone\n\n先來看一個例子 [docker-compose-standalone-firefox.yml](docker-compose-standalone-firefox.yml)\n\n```yml\nversion: \"3\"\nservices:\n  firefox:\n    image: selenium/standalone-firefox\n    shm_size: 2gb\n    ports:\n      - \"4444:4444\"\n      - \"7900:7900\"\n    # environment:\n    #   - SE_VNC_NO_PASSWORD=1 # NO_PASSWORD\n    #   - SE_VNC_VIEW_ONLY=1   # readonly\n```\n\n```cmd\ndocker-compose -f docker-compose-standalone-firefox.yml up\n```\n\n進入 firefox 容器可以看到 driver 已經安裝好了.\n\n```cmd\n\u003e\u003e ls -al /opt\n\ndrwxr-xr-x 7 root root    4096 Dec 19 18:09 firefox-latest\n-rwxr-xr-x 1 1000 1000 9272472 Oct 13 15:59 geckodriver-0.32.0\ndrwxrwxrwx 1 root root    4096 Dec 19 18:09 selenium\n```\n\n反之進入 chrome 容器可以也看到 driver 已經安裝,\n\n可參考 [docker-compose-standalone-chrome.yml](docker-compose-standalone-chrome.yml)\n\n接著可以進入 [http://localhost:4444/](http://localhost:4444/ui),\n查看 Selenium Grid\n\n![alt tag](https://i.imgur.com/UOZxSCg.png)\n\n接下來就是透過 python 連線,\n\n安裝 selenium\n\n```cmd\npip3 install selenium\n```\n\n程式碼可參考 [demo.py](demo.py)\n\n```python\nfrom selenium import webdriver\n\nbrowser = webdriver.Remote(\n    command_executor='http://localhost:4444/wd/hub',\n    # options=webdriver.ChromeOptions()\n    options=webdriver.FirefoxOptions()\n)\n\nbrowser.get('https://www.google.com')\nprint(browser.title)\nbrowser.save_screenshot(\"firefox.png\")\nbrowser.quit()\n```\n\n如果我們想看 firefox 執行過程, 可以進入以下的連結\n\n[http://localhost:7900/?autoconnect=1\u0026resize=scale\u0026password=secret](http://localhost:7900/?autoconnect=1\u0026resize=scale\u0026password=secret)\n\n![alt tag](https://i.imgur.com/BuA8XhA.png)\n\n預設的密碼是 secret,\n\n如果你不想要密碼, 可以透過設定 SE_VNC_NO_PASSWORD.\n\n```yml\n   environment:\n      - SE_VNC_NO_PASSWORD=1 # NO_PASSWORD\n      - SE_VNC_VIEW_ONLY=1   # readonly\n```\n\n如果不想要可以控制頁面, 可以透過設定 SE_VNC_VIEW_ONLY.\n\n這部份請參考 [Debugging](https://github.com/SeleniumHQ/docker-selenium#debugging)\n\n### Video recording\n\n也有錄影的模式可以使用,\n\n[docker-compose-video.yml](docker-compose-video.yml)\n\n\n```yml\nversion: \"3\"\nservices:\n  chrome_standalone:\n    image: selenium/standalone-chrome\n    shm_size: 2gb\n    ports:\n      - \"4444:4444\"\n      - \"6900:5900\"  # 5900 for VNC Server\n      - \"7900:7900\"\n\n  chrome_video:\n    image: selenium/video:ffmpeg-4.3.1-20221219\n    volumes:\n      - ./videos:/videos\n    depends_on:\n      - chrome_standalone\n    environment:\n      - DISPLAY_CONTAINER_NAME=chrome_standalone\n      - FILE_NAME=chrome_video.mp4\n```\n\n當你關閉/停止容器的時候, 會將影片寫入 videos 資料夾,\n\n影片會有全部操作的過程.\n\n### Hub and Nodes\n\n除了 Standalone 之外, 還有另一種 Hub and Nodes,\n\n[docker-compose-v3.yml](docker-compose-v3.yml)\n\n```yml\n# To execute this docker-compose yml file use `docker-compose -f docker-compose-v3.yml up`\n# Add the `-d` flag at the end for detached execution\n# To stop the execution, hit Ctrl+C, and then `docker-compose -f docker-compose-v3.yml down`\nversion: \"3\"\nservices:\n  chrome:\n    image: selenium/node-chrome:4.7.2-20221219\n    shm_size: 2gb\n    depends_on:\n      - selenium-hub\n    environment:\n      - SE_EVENT_BUS_HOST=selenium-hub\n      - SE_EVENT_BUS_PUBLISH_PORT=4442\n      - SE_EVENT_BUS_SUBSCRIBE_PORT=4443\n\n  firefox:\n    image: selenium/node-firefox:4.7.2-20221219\n    shm_size: 2gb\n    depends_on:\n      - selenium-hub\n    environment:\n      - SE_EVENT_BUS_HOST=selenium-hub\n      - SE_EVENT_BUS_PUBLISH_PORT=4442\n      - SE_EVENT_BUS_SUBSCRIBE_PORT=4443\n\n  selenium-hub:\n    image: selenium/hub:4.7.2-20221219\n    container_name: selenium-hub\n    ports:\n      - \"4442:4442\"\n      - \"4443:4443\"\n      - \"4444:4444\"\n```\n\n這樣子就可以選擇要用哪個瀏覽器測試了.\n\n這邊有兩個地方稍微說明一下,\n\n第一是假如你有 session 沒有正確登入, 你會發現你沒有辦法再連結上去,\n\n除非等到 [session-timeout](https://github.com/SeleniumHQ/docker-selenium#grid-url-and-session-timeout), 或是重起容器.\n\n第二是這邊沒有 7900 看畫面, 但是你可以從 session 裡面的照相機圖示\n\n點進去一樣可以瀏覽 UI.\n\n![alt tag](https://i.imgur.com/uUsvEJE.png)\n\n### Running in Headless mode\n\n請參考 [Running in Headless mode](https://github.com/SeleniumHQ/docker-selenium#running-in-headless-mode)\n\n## 小結論\n\n基本上我覺得這個東西真的不錯用, 可以用來測試或開發應該都沒有什麼問題,\n\n簡單說, 推薦使用, 以後終於不用再擔心 chrome 版本和 driver 不符合了:smile:\n\n## Donation\n\n文章都是我自己研究內化後原創，如果有幫助到您，也想鼓勵我的話，歡迎請我喝一杯咖啡:laughing:\n\n綠界科技ECPAY ( 不需註冊會員 )\n\n![alt tag](https://payment.ecpay.com.tw/Upload/QRCode/201906/QRCode_672351b8-5ab3-42dd-9c7c-c24c3e6a10a0.png)\n\n[贊助者付款](http://bit.ly/2F7Jrha)\n\n歐付寶 ( 需註冊會員 )\n\n![alt tag](https://i.imgur.com/LRct9xa.png)\n\n[贊助者付款](https://payment.opay.tw/Broadcaster/Donate/9E47FDEF85ABE383A0F5FC6A218606F8)\n\n## 贊助名單\n\n[贊助名單](https://github.com/twtrubiks/Thank-you-for-donate)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftwtrubiks%2Fdocker-selenium-tutorial","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftwtrubiks%2Fdocker-selenium-tutorial","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftwtrubiks%2Fdocker-selenium-tutorial/lists"}