Skip to content

Instantly share code, notes, and snippets.

@webgyo
Created February 16, 2018 10:16
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save webgyo/baa72dca8cbee6080b41ea2d14aebaad to your computer and use it in GitHub Desktop.
Save webgyo/baa72dca8cbee6080b41ea2d14aebaad to your computer and use it in GitHub Desktop.
find_element系で要素がないときエラーになる #python

find_element系で指定した要素が無いとループ処理がエラー(NoSuchElementException)で止まってしまうのでエラーをハンドリングする

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
a = post.find_element_by_css_selector("a").text

上記を以下のように変更

try:
	a = post.find_element_by_css_selector("a").text
except NoSuchElementException:
	#ここにエラー時の代替処理
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment