Selenium to understand in 10 minutes was very helpful, so I made a small sequel without permission.
[Method 1 of "Selenium to understand in 10 minutes"](https://qiita.com/Chanmoro/items/9a3c86bb465c1cce738a#%E6%96%B9%E6%B3%951-docker-%E3%81%A7% According to E7% 92% B0% E5% A2% 83% E6% A7% 8B% E7% AF% 89), it takes time to build the environment by connecting to the Selenium Docker container that was created and moved earlier. You can easily mess with Selenium without having to. However, as it is, the Docker container will continue to work even after using the Selenium script, and you will have to quit it yourself.
In this article, we will automate the process of stopping and removing the Docker container after running the script. So ** It's a lie that this article is a Selenium article. ** Title fraud. sorry.
This article will show you a nice way to work with Docker container with Docker SDK for Python based on Selenium. ..
TL;DR First, install Docker SDK for Python.
pip install docker
import docker
import time
from selenium import webdriver
from contextlib import contextmanager
SELENIUM_IMAGE = 'selenium/standalone-chrome:3.141.59-xenon'
@contextmanager
def selenium_container():
client = docker.from_env()
try:
selenium_image = client.images.get(SELENIUM_IMAGE)
except docker.errors.ImageNotFound:
selenium_image = client.images.pull(SELENIUM_IMAGE)
container = client.containers.run(
selenium_image, detach=True, remove=True,
ports={4444: 4444}, shm_size='2g')
try:
while b'Selenium Server is up' not in container.logs():
time.sleep(0.1)
yield container
finally:
container.kill()
@contextmanager
def selenium_driver():
options = webdriver.ChromeOptions()
options.add_argument('--headless')
driver = webdriver.Remote(
command_executor='http://localhost:4444/wd/hub',
desired_capabilities=options.to_capabilities(),
options=options,
)
try:
yield driver
finally:
driver.quit()
def test():
with selenium_container(), selenium_driver() as driver:
driver.get('https://qiita.com')
print(driver.current_url)
if __name__ == '__main__':
test()
contextmanager
@contextmanager
def selenium_container():
I used contextlib.contextmanager to terminate container successfully even if an error occurs. The same is true for webdriver.
client = docker.from_env()
try:
selenium_image = client.images.get(SELENIUM_IMAGE)
except docker.errors.ImageNotFound:
selenium_image = client.images.pull(SELENIUM_IMAGE)
First, get the Docker image of Selenium. If you haven't pulled it yet, pull it first.
docker run
container = client.containers.run(
selenium_image, detach=True, remove=True,
ports={4444: 4444}, shm_size='2g')
This is equivalent to docker run -d -p 4444: 4444 --shm-size = 2g selenium / standalone-chrome: 3.141.59-xenon
.
docker kill
try:
while b'Selenium Server is up' not in container.logs():
time.sleep(0.1)
yield container
finally:
container.kill()
Since we're passing detach = True
,client.containers.run ()
moves to the next step as soon as we launch Docker container. Therefore, Webdriver is not yet ready at this stage. First you have to wait until it's ready.
When the preparation is completed, the Docker container log will output 06: 53: 00.628 INFO [SeleniumServer.boot] --Selenium Server is up and running on port 4444
, etc., so turn the while loop until you see this output. Wait [^ 1].
[^ 1]: Docker SDK for Python documentation has logs () It is written that
returns str
, but please note that it returns bytes
for some reason at the time of writing the article.
Yield container when ready. This yielded container can be referenced as with selenium_container () as container:
.
The processing of selenium_driver
is paused when the container is yielded and waits until it exits the with
block.
If the process completes or an error occurs inside the with
block and exits the with
block, the selenium_driver
will start again and thecontainer.kill ()
of the finally
block will be killed. I will do it.
Now you don't have to worry about forgetting to quit Docker container or Webdriver. Have a good Selenium life!
Recommended Posts