Python Selenium ヘッドレス: Chrome ブラウザをヘッドレス モードで開く
このチュートリアルでは、Python で Selenium を使用してブラウザーのヘッドレス モードを実行する方法について説明します。
Python で Selenium を使用して Chrome ブラウザをヘッドレス モードで実行する
ヘッドレス ブラウザについて言えば、それらを実際のブラウザと呼ぶこともできますが、それらはバックグラウンドで実行されています。 それらはどこにも表示されませんが、バックグラウンドで実行されています。
このヘッドレス ブラウザが必要になるシナリオがいくつかあります。
通常のブラウザで作業していると、ローカル システムで作業しているときに UI が表示され、他のアプリケーションを操作していることがわかります。 したがって、他の操作を実行することはできず、追加の操作が目の前で実行されます。
スクリプトをヘッドレス モードで実行するとします。 作業を続行できるように、phantomJS、HtmlUnit などのヘッドレス モードを使用できるブラウザーがいくつかあります。
Chrome と Firefox にはヘッドレス オプションもあります。 Selenium with Chrome でヘッドレス モードでテストを実行する方法を確認するには、まず Python ファイルを作成する必要があります。
いくつかの必要なクラスとモジュールをインポートして、コードにジャンプします。
import time
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
以下に示すように、webdriver.Chrom() にはさまざまなオプションがあることに気付きました。

Chrome セッションを開始する service 引数を使用しました。 ChromeDriverManager() は、ドライバーをダウンロードして パス を設定するのに役立ちます。
get() メソッドを使用して、検索ボックスを見つけようとする URL を渡します。次に、find_element() を使用して検索するランダム テキストを使用します。
DV = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
DV.get("http://www.google.com")
DV.find_element(By.NAME, "q").send_keys("Elon Musk")
time.sleep(2)
Python スクリプトを実行すると、ヘッドレス モードで実行されないことがわかり、検索ボックスに移動して、指定されたクエリを検索します。

次に、options という 2 番目の引数を使用し、options を提供する必要があります。 さまざまなパッケージの options クラスがあります。 Opera、Chrome、および Firefox で使用できます。
Chrome を使用しているため、Chrome.options の Options() クラスを使用します。したがって、OP という名前のオブジェクトを作成して、Options() クラスを呼び出します。
ヘッドレス モードでテストを実行するには、2つの オプション または異なる方法があります。 まず、add_argument() メソッドを使用し、その中に --headless を渡す必要があります。
この効果を得るには、OP を options パラメータに渡す必要があります。
import time
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
OP = Options()
OP.add_argument("--headless")
DV = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=OP)
DV.get("http://www.google.com")
DV.find_element(By.NAME, "q").send_keys("Elon Musk")
完了すると、テストがヘッドレス モードで実行され、何も表示されなくなります。

検索されたかどうかを確認するために、get_screenshot_as_file() メソッドを使用してヘッドレス モードでスクリーンショットをキャプチャします。 ブラウザをヘッドレス モードで開き、すぐにスクリーンショットを取得します。
DV.get_screenshot_as_file(os.getcwd() + "/screenshot.png")
これで、バックグラウンドで何が起こっているかを示すスクリーンショットが得られました。

あなたが一般的にスペルミスをする人で、時々 OP.add_argument('--headless') を覚えるのが難しいとします。 次に、もう 1つのオプションがあります。headless という type プロパティです。
デフォルトではFalseに設定されており、Trueに変更できるため、add_argument()を使用する必要はありません。
import time
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
import os
OP = Options()
OP.headless = True
# OP.add_argument('--headless')
DV = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=OP)
DV.get("http://www.google.com")
# DV.find_element(By.NAME,'q').send_keys('Elon Musk')
DV.find_element(By.NAME, "q").send_keys("mark zuckerberg")
DV.get_screenshot_as_file(os.getcwd() + "/screenshot.png")
このスクリプトを再実行すると、バックグラウンドで実行されている操作のスクリーンショットが得られます。

Hello! I am Salman Bin Mehmood(Baum), a software developer and I help organizations, address complex problems. My expertise lies within back-end, data science and machine learning. I am a lifelong learner, currently working on metaverse, and enrolled in a course building an AI application with python. I love solving problems and developing bug-free software for people. I write content related to python and hot Technologies.
LinkedIn