在python中获取下拉框选中的值需要使用python的第三方库,如Selenium,BeautifulSoup等。
如果使用Selenium,可以使用如下代码获取下拉框选中的值:
from selenium import webdriver
browser = webdriver.Firefox()
browser.get('https://www.example.com/')
select = browser.find_element_by_id('select-id')
selected_option = select.find_element_by_xpath("//option[@selected='selected']")
value = selected_option.get_attribute('value')
print(value)
browser.quit()
如果使用BeautifulSoup,可以使用如下代码获取下拉框选中的值:
import requests
from bs4 import BeautifulSoup
res = requests.get('https://www.example.com/')
soup = BeautifulSoup(res.text, 'html.parser')
select = soup.find('select', {'id': 'select-id'})
selected_option = select.find('option', {'selected': 'selected'})
value = selected_option['value']
print(value)
这只是一种获取下拉框选中值的方法,你还可以选择其他方法。