ログを使用すると、スクリプトの進行状況を追跡し、スクリプトの実行中に何が起こっているかを把握することができます。 

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 パッケージをインポートするには、次のようにします。

import logging
PY

logging パッケージをインポートすると、さまざまなタイプのログを生成できます。

print("This is a print statement.")
logging.info("This is an info message")
logging.warn("This is a warning message")
logging.debug("This is a debug message")
logging.error("This is an error message")
PY

さまざまなタイプのエラーは、Session Details ダイアログのウォーターフォールで色分けされます。

Session Details

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

Script Output