파이썬을 윈도우에서 실행하면 눈으로 실행 과정을 지켜 볼 수가 있습니다. 인터넷 상에서 다루고 있는 많은 포스팅이나 강좌들도 윈도우에서 selenium을 실행하는 기준으로 설명을 하고 있습니다. 저는 시놀로지 위에서 돌고 있는 code server (VS Code)에서 파이썬을 실행하고 있기에, 리눅스 상에서 selenium 실행이 필요했습니다. 앞서 포스팅 했던 codercom code server의 경우 chrome이 설치되어 있지 않으며 UI도 지원하지 않아 selenium을 활용하여 개발하기가 쉽지 않습니다. 이를 해결하기 위해 selenium을 도커로 설치하여 remote로 사용하는 방법을 알아보도록 하겠습니다.
selenium 설치
시놀로지 shell상에서 아래 명령어로 selenium 도커를 설치합니다. 필요에 따라서 포트번호나 설정 등은 변경해 주세요.
docker run -d --name selenium_firefox \\
-p 4444:4444 -p 5900:5900 -p 7900:7900 \\
--shm-size 2g seleniarm/standalone-firefox:latest
위 명령어로 docker를 설치하고 실행하였다면, 아래 주소를 통해 브라우저를 접속하고 실행되고 있는 페이지를 볼 수 있습니다.
http://{SERVER_IP}:4444/wd/hub
python 에서 selenium 도커 사용
위의 주소를 _host 변수에 저장하고 아래와 같이 코드를 사용하면 local/remote에서 동시에 사용할 수 있는 코드 작성이 가능합니다.
if _mode == 'local':
from selenium.webdriver.chrome.options import Options
options = Options()
if _headless:
options.add_argument('headless')
self.driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
elif _mode == 'remote':
from selenium.webdriver.firefox.options import Options
options = Options()
options.set_preference("general.useragent.override", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36")
options.set_preference("general.platform.override", "Win32")
self.driver = webdriver.Remote(_host, options=options)