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
driver.execute_script("return document.getElementsByClassName('field_select')[0].value;")
Resolved in
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()
© 2024 OneMinuteCode. All rights reserved.