我有一个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类,然后循环它的option标签获取文本。
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]