python+selenium遍历下拉选择框中的所有下拉选项
下拉选择框:基于VUE的标签选择器,可见网址:https://www.antdv.com/components/select-cn#components-select-demo-tags
我想获取到如图红框中的所有数据
初学者一枚,希望可以得到指教,谢谢了!
【以下回答由 GPT 生成】
解决方案:
要解决这个问题,我们可以使用Selenium模拟滚动页面的操作,直到所有的下拉选项都被加载并显示出来。以下是具体的解决步骤和代码示例。
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from time import sleep
options = webdriver.ChromeOptions()
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
driver = webdriver.Chrome(chrome_options=options)
driver.maximize_window()
url = 'https://www.antdv.com/components/select-cn#components-select-demo-tags'
driver.get(url)
driver.implicitly_wait(10) # 设置隐式等待时间为10秒,等待页面加载完成
dropdown = driver.find_element_by_xpath('//div[@id="components-select-demo-tags"]//div[@class="ant-select-selector"]')
dropdown.click() # 点击下拉选项激活下拉列表
sleep(2) # 等待下拉列表的选项加载完成
# 使用键盘的下箭头键自动滚动页面,直到所有选项都显示出来
actions = ActionChains(driver)
while True:
actions.send_keys(Keys.ARROW_DOWN).perform()
sleep(1)
# 如果所有选项都已加载成功,则跳出循环
if "加载更多" not in driver.page_source:
break
options = driver.find_elements_by_xpath('//div[@id="components-select-demo-tags"]//ul[@role="listbox"]/li')
for option in options:
print(option.text)
driver.quit()
这样,你就可以通过Selenium模拟滚动页面的操作,来获取到基于VUE的标签选择器中的所有下拉选项了。
请注意,以上代码示例是基于Python和Selenium的,在运行代码之前,请确保已经安装了Python和Selenium,并下载了适合你操作系统的Chrome浏览器驱动程序。
希望以上的解决方案能对你有所帮助,如果你还有其他问题或需要进一步的帮助,请及时告诉我。