1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Seleniumでチェックボックスにチェックする

Posted at

Seleniumでチェックボックスを扱う

Seleniumでチェックボックスの状態を読み、チェックが入っていない時はチェックを入れたいときがあった。
…が、どうもそんな方法は無いというのをちらほら見て、まさかと思っていたけど本当になかった。

どうしたら良いものかと探していたら、JavaScriptを使ってチェックを入れる(というかクリックする)と良いというのを見たので実践。
driverはおなじみChromeDriverを使っている。

xpath = '//*[@id="checkbox1"]'
chkbox = driver.find_element_by_xpath(xpath)

# チェックボックスが非選択の状態を確認
if not chkbox.is_selected():
    # 非選択の場合はJavaScriptでクリックする
    driver.execute_script("arguments[0].click();", chkbox)

これを実行すると確かにチェックボックスの操作が出来た。
driver.execute_script(foo)でJavaScriptが実行できるのは、使い道は多そう。

1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?