質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

1回答

36876閲覧

seleniumでスクレイピング--click()でエラーが発生する

SaikoSaiko

総合スコア15

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2018/02/28 14:40

前提・実現したいこと

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/ツールのバージョンなど)

よろしくお願いいたします

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

ElementNotInteractableExceptionが発生していますが、この例外は、要素は`DOPMツリーには存在するが対話(操作)できない状態であることを意味しています。
対策としては5.1. 明示的な待機に示されているように、操作できるまで待機するとよいようです。

以下はchromedriverにてelement_to_be_clickableまで待機する例です。
FireFoxでうまく行かない場合は、visibility_of_element_locatedなど他の条件で試してみるとよいでしょう。

Python

1from selenium import webdriver 2from selenium.webdriver.common.by import By 3from selenium.webdriver.support.ui import WebDriverWait 4from selenium.webdriver.support import expected_conditions 5import time 6 7driver = webdriver.Chrome("chromedriver") 8driver.get("http://www.yahoo.co.jp") 9 10#elem = driver.find_element_by_link_text(u"ニュース") 11wait = WebDriverWait(driver, 10) # 最大10秒 12elem = wait.until( expected_conditions.element_to_be_clickable( (By.LINK_TEXT,"ニュース")) ) 13elem.click() 14 15time.sleep(5) 16driver.close()

参考:
Selenium web driver: cannot be scrolled into view
Python】visibility_of_element_located・・・指定した要素が表示されるまで待機する

投稿2018/03/01 00:55

can110

総合スコア38234

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

SaikoSaiko

2018/03/01 03:39 編集

解決できました! 教えていただきたいのですが,ElementNotInteractableExceptionからどのようにして解決策が載っっているサイトに辿りついたのでしょうか?
can110

2018/03/01 04:05

「ElementNotInteractableException」や「could not be scrolled into view」でstackoverflowに到達。 あとは「WebDriverWait」でその他のページにたどり着きました。 まずはエラーメッセージから検索すると、たいていは対処法が見つかります。
SaikoSaiko

2018/03/01 04:15

勉強になりました. ありがとうございました.
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問