How to get the current value of Selenium, Python 3, Combo Box

Asked 1 years ago, Updated 1 years ago, 83 views

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('Hello')

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)

If so,

Afrikaans
Albanian
Arabic
Armenian
Bengali (Bangladesh)
Bengali (India)

… is printed, but how do I get the value of the combo box currently selected?

python python3 selenium selenium-webdriver

2022-09-30 21:37

2 Answers

driver.execute_script("return document.getElementsByClassName('field_select')[0].value;")

Resolved in


2022-09-30 21:37

Now that you've got Select, you can get all the option elements selected by the following methods:

If it's a single choice, why don't we do the following?

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('Hello')

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()


2022-09-30 21:37

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.