How get the selected text option with Selenium using Python?
Question:
I have this option in a web page
I am trying to extract the selected text in the SELECTBOX with this code:
time.sleep(5)
elemento2 = Select(WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.NAME, "jform[cod_comune]"))))
selected_option = elemento2.first_selected_option
print(selected_option.text)
but I get nothing, not even errors. The text is empty.
Answers:
I wouldn’t use the first selected property. I recommend getting the xpath of the span tag. Then using the selenium .text
to get the text for the field. This works for most websites
selected_option = elemento2.text
Basic way:
driver.get(url)
time.sleep(3)
text = driver.find_element_by_xpath(xpath).text
I have this option in a web page
I am trying to extract the selected text in the SELECTBOX with this code:
time.sleep(5)
elemento2 = Select(WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.NAME, "jform[cod_comune]"))))
selected_option = elemento2.first_selected_option
print(selected_option.text)
but I get nothing, not even errors. The text is empty.
I wouldn’t use the first selected property. I recommend getting the xpath of the span tag. Then using the selenium .text
to get the text for the field. This works for most websites
selected_option = elemento2.text
Basic way:
driver.get(url)
time.sleep(3)
text = driver.find_element_by_xpath(xpath).text