Python selenium 从下拉列表中复制所有选项

Python selenium copy all options from drop down list

我有一个 html,如下所示:

<select class="v-select-select" size="1" tabindex="0"><option value="1">opt1_AGG</option><option value="2">opt2_AGG</option></select>

这是下拉列表的一部分,我可以访问 xml 路径信息。所以,我想尝试这样的事情:

from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait, Select
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
xpathLoc = r'/html/body/div[1]/div/div[2]/div/div[2]/div/div[2]/div/div/div[3]/div/div/div[1]/div/div/div[1]/div/table/tbody/tr[3]/td[3]/div/select'
browser = webdriver.Chrome(executable_path=path_to_chromedriver)
    wait = WebDriverWait(browser, int(np.random.uniform(low=10, high=15, size=(1,))[0]))
browser.get("mysite.com")
wait.until(EC.presence_of_element_located((By.XPATH, xpathLoc)))

我想在列表中获取以下信息,所需的输出变量是:

['opt1_AGG', 'opt2_AGG']

我不确定如何进行。

您可以执行以下操作。使用 v-select-select 获取 select class,然后循环遍历它的选项标签获取文本。

select_box = browser.find_element_by_xpath("//select[@class='v-select-select']")   
options = [x.text for x in select_box.find_elements_by_tag_name("option")]

或者

from selenium.webdriver.support.ui import Select 
select_box = Select(browser.find_element_by_xpath("//select[@class='v-select-select']"))
options = [x.text for x in select_box.options]