1
3

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 Python VBA プルダウンで選択されている値を取得する

Posted at

Selenium Python Vba プルダウン(Select)で選択されている値を取得する

検索しても、Selectに値を設定するものばかりで、みつけるのに多少時間がかかったのでこちらでもメモします。

前提
Windows
Python 3.8
Selenium は既に使えていて、 driver の変数で扱うものとする

Selenium Python プルダウンで選択されている値を取得する

Selectクラスを使います。

なのでSelectクラスをインポートする必要があります。
Select要素を参照するWebElementをSelect型にすると、様々なことができます。

Select要素を操作する詳しい説明公式サイト
https://www.selenium.dev/documentation/ja/support_packages/working_with_select_elements/

プルダウンから選択されている値を取得する
from selenium.webdriver.support.select import Select
# Select要素を操作するにはSelectクラスをインポートする

AA = driver.find_element_by_id("A")  #IDがAのSelect要素を取得する      
AAA=Select(AA)             #Selectオブジェクトを作成 このオブジェクトから様々な操作ができます。

# プルダウンで選択されている値は
print(AAA.first_selected_option.text)

# 複数選択できるプルダウンの場合リストで取得できます。
all_selected = AAA.all_selected_options  #all_selectedはリストになります。

SeleniumBasic  VBA プルダウンで選択されている値を取得する

こちらは簡単

プルダウンで選択されている値を取得する

Dim AA As WebElement  'WebElement型で宣言

’IDがAのSelect要素を取得
Set AA = driver.FindElementById("A").AsSelect.SelectedOption

Debug.Print (A.Text)  ’選択されている値が出力されました。できたー

今日のつぶやき
そういえばテキストボックスやチェックボックスの値取得ばかりで
Selectの選択値をスクレイピングで取得することは久しぶりです。
最近また依頼が増えてきたので、スピードをもって取得していこう。
もっと考える暇がほしいです。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?