0Pricing
Web Scraping & Bots · レッスン

動的ページの待機戦略

Selenium の明示的待機、暗黙的待機、フルーエント待機を使い分け、非同期に読み込まれるコンテンツをスクレイパーで確実に処理する方法を身につけます。

「動的ページの待機戦略」はCoddyKit上の無料Web Scraping & Botsレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはWeb Scraping & Bots学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Web Scraping & Botsコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

The Timing Problem

Dynamic pages render content after the initial HTML loads. If your scraper grabs an element before JavaScript injects it, you get a NoSuchElementException or empty data.

Waiting strategies tell Selenium to pause until the page is ready, making automation reliable instead of flaky.

Why Not Just sleep()

A fixed time.sleep(5) is tempting but bad: it wastes time when the page is fast and still fails when the page is slow. Smart waits poll until a condition is true, then continue immediately.

import time
time.sleep(5)  # fragile: arbitrary, blocking, often wrong

Implicit Waits

An implicit wait sets a global timeout. Selenium retries finding any element for up to that many seconds before failing.

Simple, but it applies to all lookups and cannot wait for arbitrary conditions like visibility or text.

driver.implicitly_wait(10)  # seconds, applies globally

Explicit Waits

An explicit wait targets one specific condition. Use WebDriverWait with an expected_conditions check. This is the recommended approach for scraping.

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

element = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.ID, 'results'))
)

Common Expected Conditions

The expected_conditions module covers most needs:

  • presence_of_element_located in the DOM.
  • visibility_of_element_located is rendered and visible.
  • element_to_be_clickable ready for interaction.
  • text_to_be_present_in_element waits for content.
EC.visibility_of_element_located((By.CLASS_NAME, 'price'))
EC.element_to_be_clickable((By.ID, 'load-more'))

Waiting for Text

When you need a specific value to appear (for example a price loaded by an API call), wait on the text rather than mere presence.

WebDriverWait(driver, 15).until(
    EC.text_to_be_present_in_element((By.ID, 'status'), 'Loaded')
)

Fluent Waits

A fluent wait lets you tune the polling interval and ignore specific exceptions while waiting. Useful for slow APIs that throw transient errors.

wait = WebDriverWait(driver, timeout=20, poll_frequency=1,
                     ignored_exceptions=[StaleElementReferenceException])
wait.until(EC.presence_of_element_located((By.ID, 'data')))

Handling Stale Elements

If the DOM re-renders, a stored element reference becomes stale. Re-fetch the element inside the wait or after the page settles instead of reusing the old handle.

from selenium.common.exceptions import StaleElementReferenceException

try:
    el.click()
except StaleElementReferenceException:
    el = driver.find_element(By.ID, 'btn')
    el.click()

Waiting for Page Load State

You can poll the document's readyState via JavaScript to confirm the whole page finished loading before scraping.

WebDriverWait(driver, 10).until(
    lambda d: d.execute_script('return document.readyState') == 'complete'
)

Combining Strategies Wisely

Best practice: avoid mixing implicit and explicit waits (they compound unpredictably). Pick explicit waits for scraping, keep timeouts realistic, and wait for the precise condition your data depends on.

A Reliable Pattern

Wrap waits in a helper so every lookup is robust. This keeps your scraping code clean and consistent.

def wait_for(driver, locator, timeout=10):
    return WebDriverWait(driver, timeout).until(
        EC.visibility_of_element_located(locator)
    )

price = wait_for(driver, (By.CLASS_NAME, 'price')).text

Quick Check

Test your understanding of waiting strategies.

Recap

You learned to handle asynchronous content with implicit, explicit, and fluent waits, target conditions like visibility and clickability, recover from stale elements, and avoid fragile fixed sleeps.

Reliable waiting is the foundation of stable dynamic-page scraping.

よくある質問

「動的ページの待機戦略」レッスンは無料ですか?

はい。「動的ページの待機戦略」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Web Scraping & Botsコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Web Scraping & Botsコースには全4レッスンが含まれています。

「動的ページの待機戦略」で何を学びますか?

Selenium の明示的待機、暗黙的待機、フルーエント待機を使い分け、非同期に読み込まれるコンテンツをスクレイパーで確実に処理する方法を身につけます。 ブラウザで直接実行するハンズオンコードでWeb Scraping & Botsを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Web Scraping & Botsを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのWeb Scraping & Botsは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。

「動的ページの待機戦略」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このWeb Scraping & Botsレッスンでコードを書いて実行できますか?

はい。すべてのWeb Scraping & Botsレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. Selenium入門
  2. ブラウザー操作の自動化
  3. JavaScriptからのデータ抽出
  4. 動的ページの待機戦略
← Web Scraping & Botsに戻る