【Python】seleniumで待機する処理
昨日に続きseleniumネタ。
読み込まれるまで待機
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
def chrome():
options = webdriver.ChromeOptions()
options.binary_location = "/path/chrome"
driver = webdriver.Chrome(
executable_path="/path/chromedriver",
chrome_options=options
)
return driver
driver = chrome()
# 最大待ち時間
wait_time = 10
wait = WebDriverWait(driver, wait_time)
# ページのすべての要素が読み込まれるまで待機(タイムアウトは10秒)
wait.untile(EC.presence_of_all_elements_located)
# IDで指定した要素が読み込まれるまで待機
# ByIDの代わりにBy.CSS_SELECTOR → CSSセレクタ、By.XPATH → XPath
wait.untile(EC.presence_of_element_located((By.ID, "ID名")))
表示されるまで待機
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
def chrome():
options = webdriver.ChromeOptions()
options.binary_location = "/path/chrome"
driver = webdriver.Chrome(
executable_path="/path/chromedriver",
chrome_options=options
)
return driver
driver = chrome()
# 最大待ち時間
wait_time = 10
wait = WebDriverWait(driver, wait_time)
# IDで指定した要素が表示されるまで待機
# ByIDの代わりにBy.CSS_SELECTOR → CSSセレクタ、By.XPATH → XPath
wait.untile(EC.visibility_of_element_located((By.ID, "ID名")))