seleniumでスクレイピング--click()でエラーが発生する
解決済
回答 1
投稿
- 評価
- クリップ 0
- VIEW 16K+
前提・実現したいこと
https://a-zumi.net/python-selenium-find-element-by-link-text/
上記のサイトに記載されているプログラムを無事に実行させたい.
発生している問題・エラーメッセージ
Traceback (most recent call last):
File "test1.py", line 11, in <module>
driver.find_element_by_link_text(u"ニュース").click()
File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/webelement.py", line 80, in click
self._execute(Command.CLICK_ELEMENT)
File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/webelement.py", line 628, in _execute
return self._parent.execute(command, params)
File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/webdriver.py", line 312, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotInteractableException: Message: Element <a id="nsearch" href="https://rdsig.yahoo.co.jp/_ylt=A7dPhhDxvJZani0AEniAcQF8/RV=2/RE=1519914609/RH=cmRzaWcueWFob28uY28uanA-/RB=/RU=aHR0cHM6Ly9uZXdzLnlhaG9vLmNvLmpwLwA-/RK=0/RS=DCKK3Ar12WdVbv0nPTan5nlo9UY-"> could not be scrolled into view
Error in sys.excepthook:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 63, in apport_excepthook
from apport.fileutils import likely_packaged, get_recent_crashes
File "/usr/lib/python3/dist-packages/apport/init.py", line 5, in <module>
from apport.report import Report
File "/usr/lib/python3/dist-packages/apport/report.py", line 30, in <module>
import apport.fileutils
File "/usr/lib/python3/dist-packages/apport/fileutils.py", line 23, in <module>
from apport.packaging_impl import impl as packaging
File "/usr/lib/python3/dist-packages/apport/packaging_impl.py", line 23, in <module>
import apt
File "/usr/lib/python3/dist-packages/apt/init.py", line 23, in <module>
import apt_pkg
ModuleNotFoundError: No module named 'apt_pkg'
Original exception was:
Traceback (most recent call last):
File "test1.py", line 11, in <module>
driver.find_element_by_link_text(u"ニュース").click()
File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/webelement.py", line 80, in click
self._execute(Command.CLICK_ELEMENT)
File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/webelement.py", line 628, in _execute
return self._parent.execute(command, params)
File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/webdriver.py", line 312, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotInteractableException: Message: Element <a id="nsearch" href="https://rdsig.yahoo.co.jp/_ylt=A7dPhhDxvJZani0AEniAcQF8/RV=2/RE=1519914609/RH=cmRzaWcueWFob28uY28uanA-/RB=/RU=aHR0cHM6Ly9uZXdzLnlhaG9vLmNvLmpwLwA-/RK=0/RS=DCKK3Ar12WdVbv0nPTan5nlo9UY-"> could not be scrolled into view
該当のソースコード
python
# coding: utf-8
from selenium import webdriver
import time
# FireFoxを起動
driver = webdriver.Firefox()
# ヤフーへアクセス
driver.get("http://www.yahoo.co.jp")
# ニュースを探してクリック
driver.find_element_by_link_text(u"ニュース").click()
# 5秒待つ
time.sleep(5)
# 終了
driver.close()
試したこと
ここに問題に対して試したことを記載してください。
補足情報(FW/ツールのバージョンなど)
よろしくお願いいたします
-
気になる質問をクリップする
クリップした質問は、後からいつでもマイページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
クリップを取り消します
-
良い質問の評価を上げる
以下のような質問は評価を上げましょう
- 質問内容が明確
- 自分も答えを知りたい
- 質問者以外のユーザにも役立つ
評価が高い質問は、TOPページの「注目」タブのフィードに表示されやすくなります。
質問の評価を上げたことを取り消します
-
評価を下げられる数の上限に達しました
評価を下げることができません
- 1日5回まで評価を下げられます
- 1日に1ユーザに対して2回まで評価を下げられます
質問の評価を下げる
teratailでは下記のような質問を「具体的に困っていることがない質問」、「サイトポリシーに違反する質問」と定義し、推奨していません。
- プログラミングに関係のない質問
- やってほしいことだけを記載した丸投げの質問
- 問題・課題が含まれていない質問
- 意図的に内容が抹消された質問
- 過去に投稿した質問と同じ内容の質問
- 広告と受け取られるような投稿
評価が下がると、TOPページの「アクティブ」「注目」タブのフィードに表示されにくくなります。
質問の評価を下げたことを取り消します
この機能は開放されていません
評価を下げる条件を満たしてません
質問の評価を下げる機能の利用条件
この機能を利用するためには、以下の事項を行う必要があります。
- 質問回答など一定の行動
-
メールアドレスの認証
メールアドレスの認証
-
質問評価に関するヘルプページの閲覧
質問評価に関するヘルプページの閲覧
checkベストアンサー
+3
ElementNotInteractableExceptionが発生していますが、この例外は、要素は`DOPMツリーには存在するが対話(操作)できない状態であることを意味しています。 対策としては5.1. 明示的な待機に示されているように、操作できるまで待機するとよいようです。
以下はchromedriver
にてelement_to_be_clickable
まで待機する例です。
FireFox
でうまく行かない場合は、visibility_of_element_located
など他の条件で試してみるとよいでしょう。
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
import time
driver = webdriver.Chrome("chromedriver")
driver.get("http://www.yahoo.co.jp")
#elem = driver.find_element_by_link_text(u"ニュース")
wait = WebDriverWait(driver, 10) # 最大10秒
elem = wait.until( expected_conditions.element_to_be_clickable( (By.LINK_TEXT,"ニュース")) )
elem.click()
time.sleep(5)
driver.close()
参考:
Selenium web driver: cannot be scrolled into view
Python】visibility_of_element_located・・・指定した要素が表示されるまで待機する
投稿
-
回答の評価を上げる
以下のような回答は評価を上げましょう
- 正しい回答
- わかりやすい回答
- ためになる回答
評価が高い回答ほどページの上位に表示されます。
-
回答の評価を下げる
下記のような回答は推奨されていません。
- 間違っている回答
- 質問の回答になっていない投稿
- スパムや攻撃的な表現を用いた投稿
評価を下げる際はその理由を明確に伝え、適切な回答に修正してもらいましょう。
15分調べてもわからないことは、teratailで質問しよう!
- ただいまの回答率 88.20%
- 質問をまとめることで、思考を整理して素早く解決
- テンプレート機能で、簡単に質問をまとめられる
2018/03/01 12:38 編集
教えていただきたいのですが,ElementNotInteractableExceptionからどのようにして解決策が載っっているサイトに辿りついたのでしょうか?
2018/03/01 13:05
あとは「WebDriverWait」でその他のページにたどり着きました。
まずはエラーメッセージから検索すると、たいていは対処法が見つかります。
2018/03/01 13:15
ありがとうございました.