LoginSignup
41
28

More than 3 years have passed since last update.

Seleniumでelement not interactableって怒られる場合でもエレメントを押下する方法

Last updated at Posted at 2019-01-10

ユーザーが特定のボタンを押下すると表示するカレンダー上のボタン(エレメントはspan)をseleniumで押下するために以下のようなコードを実装して動かして見たところ

# 事前にボタンを押下して対象のカレンダーを表示済み
driver.find_elements_by_xpath("/PATH/TO/ELEMENT").click()

element not interactable

って怒られる。
エレメントの属性を確認していると、is_displayed()の戻り値はFalse。

element = driver.find_elements_by_xpath("/PATH/TO/ELEMENT")
driver.execute_script("arguments[0].style.visibility='visible';", item)
driver.execute_script("arguments[0].style.display='inline-block';", item)
element.click()

とか頑張って見てもis_displayed()の戻り値はFalseのまま。

presence_of_element_locatedを使ってWaitをかけてもタイムアウトして一向に表示扱いになる気配なし。
そもそもブラウザ上での操作を見ていると対象のエレメントは表示されているし、きちんとブラウザの表示領域内に収まっている。
無論、自分で操作したら問題なく押下できている。

仕方ないのでアプローチを変更してエレメントのある領域を押下するという方法で今回は実現できた。

from selenium import webdriver
...
element = driver.find_elements_by_xpath("/PATH/TO/ELEMENT")
action = webdriver.common.action_chains.ActionChains(driver)
action.move_to_element_with_offset(element, 5, 5)
action.click()
action.perform()

無理やり感がちょっと強いが動くことが大事なテスト用のコードなので大目に見る。

41
28
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
41
28