LoginSignup
69
55

More than 5 years have passed since last update.

seleniumにてButtonがクリックできない時の対処法

Last updated at Posted at 2017-09-28

環境

python 2.7(2019/5追記、多分3系統でも)
使用OS:OSX
環境ブラウザ:Google Chrome

imports
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

Buttonが反応しない

pythonを使ってseleniumを動かしていた時のこと。
下記のようにxpathを使いtype:'button'の要素を指定してclickしてみたがなぜかエラーが

driver.find_element_by_xpath('XPATH').click()

selenium.common.exceptions.WebDriverException: Message: unknown error: Element is not clickable at point (741, 1767)

この要素はclickできないと言うのだ。
表示されているchrome上でclickしてみたが普通にできるし動く。

試しにこのボタンをちゃんとfindできているのか確認ついでにclickableか聞いてみた

if EC.element_to_be_clickable(By.XPATH, 'XPATH'):
    print 'true'
    driver.find_element_by_xpath('XPATH').click()

trueと表示された上で同様のエラーメッセージが表示された。なんでや

問題解決

driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")

この一行を直前に追加したら動くようになった
ここを参考にスクロールをさせてみた。
https://selenium-python.a-zumi.net/faq.html

どうやらブラウザ上にて画面外にあるButtonはclickできないようだ。

似たような問題

https://stackoverflow.com/questions/35126293/how-to-set-text-into-textarea-with-selenium-webdriver/35128666#35128666
これを見る限りではもしかしたらsendKeysも画面外にある場合は動かないのかも、、、
自分のケースでも対象がtype:submit()の場合は画面外に存在しても動いたため、全体的に人間が画面を通してブラウザを操作するのととても近いらしい。
画面内に対象があるかどうかは結構大事な問題なようだ。

69
55
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
69
55