Python を使用して Selenium でドロップダウン メニューの値を選択するにはどうすればよいでしょうか? 質問する

Python を使用して Selenium でドロップダウン メニューの値を選択するにはどうすればよいでしょうか? 質問する

ドロップダウンメニューから要素を選択する必要があります。

例えば:

<select id="fruits01" class="select" name="fruits">
  <option value="0">Choose your fruits:</option>
  <option value="1">Banana</option>
  <option value="2">Mango</option>
</select>

1)まずそれをクリックする必要があります。次のようにします。

inputElementFruits = driver.find_element_by_xpath("//select[id='fruits']").click()

2)その後、適切な要素を選択する必要がありますMango

試してみましたinputElementFruits.send_keys(...)が、うまくいきませんでした。

ベストアンサー1

セレンは便利なSelectクラス構成要素を操作するにはselect -> option:

from selenium import webdriver
from selenium.webdriver.support.ui import Select

driver = webdriver.Firefox()
driver.get('url')

select = Select(driver.find_element_by_id('fruits01'))

# select by visible text
select.select_by_visible_text('Banana')

# select by value 
select.select_by_value('1')

参照:

おすすめ記事