ログを使用すると、スクリプトの進行状況を追跡し、スクリプトの実行中に何が起こっているかを把握することができます。
print ステートメント
メッセージをログに記録する最も簡単な方法は、print ステートメントです。これらを使用して、オブジェクトを検査したり、問題がある場所を特定したりできます。
たとえば、オブジェクトの内容を表示することによって、ページで要素が選択されたかどうかを確認できます。
elem = driver.find_element_by_id("wiki-content")
print(elem)
PY
スクリプトの結果が未完了の場合は、次の一連の print ステートメントを使用することにより、問題が発生した可能性がある箇所を特定できます。
print("Getting the web page 'appdynamics.com'.")
driver.get("http://docs.appdynamics.com")
print("Getting the content for the container with the ID 'wiki-content'.")
elem = driver.find_element_by_id("wiki-content")
element = driver.find_element_by_xpath("//select[@name='name']")
all_options = element.find_elements_by_tag_name("option")
for option in all_options:
print("Value is: %s" % option.get_attribute("value"))
option.click()
PY
logging パッケージ
logging
パッケージをインポートして、さまざまなタイプのエラーメッセージをログに記録できます。
logging
パッケージをインポートし、ログレベルを DEBUG
に設定するには、次のようにします。
import logging
logging.getLogger().setLevel(logging.DEBUG)
PY
logging
パッケージをインポートすると、さまざまなタイプのログを生成できます。
print('Starting the monitoring test')
pageUrl = "https://www.appdynamics.com"
driver.get(pageUrl)logging.info("info message")
logging.debug("Debug message")
logging.warning("Warning message")
logging.error("error message")print('Landed on page successfully!')
PY
さまざまなタイプのエラーは、Session Details ダイアログのウォーターフォールで色分けされます。

スクリプトの出力では、次に示すように、さまざまなログメッセージが重大度別にラベル付けされます。
