
[code]

    from selenium.webdriver import Chrome, ChromeOptions
    from selenium.webdriver.common.keys import Keys
    from selenium.webdriver.support.ui import Select, WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.common.by import By
    
    options = ChromeOptions()
    options.add_argument('--headless')
    driver = Chrome(options=options, 
    executable_path='.\chromedriver.exe')
    
    driver.get('https://soundoftext.com/')
    
    elem_t = driver.find_element_by_class_name("field__textarea")
    elem_t.send_keys('こんにちは')
    
    elem_c = driver.find_element_by_class_name("field__select")
    elem_c_s = Select(elem_c)
    elem_c_s.select_by_value("ja-JP")
    
    a = driver.find_element_by_xpath('//[@id="app"]/div[1]/div/form/div[2]/select').text
    print(a)
    
[/code]

とすると、

[code]

    Afrikaans
    Albanian
    Arabic
    Armenian
    Bengali (Bangladesh)
    Bengali (India)
    
[/code]

…と出力されますが，現在選択されているコンボボックスの値を取得するにはどうすればよいでしょうか？


CC BY-SA 4.0
2018-12-04T13:05:53.560
50901
2022-05-21T15:00:30.053
2018-12-04T13:23:00.037
19110
31289
question
python python3 selenium selenium-webdriver
Selenium, Python3, コンボボックスの現在の値の取得方法

[code]

    driver.execute_script("return document.getElementsByClassName('field__select')[0].value;")
    
[/code]

で解決


CC BY-SA 4.0
2018-12-04T14:32:40.637
50906
2018-12-04T14:32:40.637
31289
50901
answer


Select を取得しているのですから以下のメソッドで選択されているoption要素がすべて取得できます。

  * [Select#all_selected_options](https://www.seleniumqref.com/api/python/element_infoget/Python_all_selected_options.html)

単一選択なら以下のようにすればよいのでないでしょうか？

[code]

    from selenium.webdriver import Chrome, ChromeOptions
    from selenium.webdriver.common.keys import Keys
    from selenium.webdriver.support.ui import Select, WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.common.by import By
    
    options = ChromeOptions()
    options.add_argument('--headless')
    driver = Chrome(options=options, 
    executable_path='.\chromedriver.exe')
    
    driver.get('https://soundoftext.com/')
    
    elem_t = driver.find_element_by_class_name("field__textarea")
    elem_t.send_keys('こんにちは')
    
    elem_c = driver.find_element_by_class_name("field__select")
    elem_c_s = Select(elem_c)
    elem_c_s.select_by_value("ja-JP")
    
    print(elem_c_s.all_selected_options[0].text)
    
    driver.quit()
    
[/code]


CC BY-SA 4.0
2019-07-16T02:21:01.697
56670
2019-07-16T02:21:01.697
27410
50901
answer