如何使用硒python取消多级下拉列表[相关选择]

时间:2019-06-01 10:33:40

标签: python selenium-webdriver drop-down-menu dropdown

我想从依赖于多级选择的下拉菜单中提取一些数据,谁能告诉我如何使用硒选择和单击项目,以便子下拉列表可能会自动更新!

您可以访问源以供参考,也可以运行代码以查看输出,一旦单击它就不会选择项目。

我写了几个代码,这里就是其中之一!

from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By
import csv
import time

//#from selenium.webdriver.support import Select 

print ("opening chorome....")  
driver = webdriver.Chrome()
driver.get('https://www.wheelmax.com/')
time.sleep(10)

csvData = ['Year', 'Make', 'Model', 'Body', 'Submodel', 'Size']

Yindex = Mkindex = Mdindex = Bindex = Smindex = Sindex = 0
print ("waiting for program to set variables....")
time.sleep(10)

print ("initializing and setting variables....")

Year = Select(driver.find_element_by_id("icm-years-select"))
for year in driver.find_elements(By.XPATH, '//*[@id="icm-years-select"]'):
    Year.select_by_index(++Yindex)
    year.click()
    time.sleep(1)
    Make = Select(driver.find_element_by_id("icm-makes-select"))
    for make in driver.find_elements(By.XPATH, '//*[@id="icm-makes-select"]'):
        Make.select_by_index(++Mkindex)
        make.click()
        time.sleep(1)
        Model = Select(driver.find_element_by_id("icm-models-select"))
        for model in driver.find_elements(By.XPATH, '//*[@id="icm-models-select"]'):
            Model.select_by_index(++Mdindex)
            model.click()
            time.sleep(1)
            Body = Select(driver.find_element_by_id("icm-drivebodies-select"))
            for body in driver.find_elements(By.XPATH, '//*[@id="icm-drivebodies-select"]'):
                Body.select_by_index(++Bindex)
                body.click()
                time.sleep(1)
                Submodel = Select(driver.find_element_by_id("icm-submodels-select"))
                for subm in driver.find_elements(By.XPATH, '//*[@id="icm-submodels-select"]'):
                    Submodel.select_by_index(++Smindex)
                    subm.click()
                    time.sleep(1)
                    Size = Select(driver.find_element_by_id("icm-sizes-select"))
                    for size in driver.find_elements(By.XPATH, '//*[@id="icm-sizes-select"]'):
                        Size.select_by_index(++Sindex)
                        csvData.append([year.text, make.text, model.text, body.text, subm.text, size.text])

1 个答案:

答案 0 :(得分:0)

尝试一下:

选择多级依赖下拉菜单选项

selectYear = Select(driver.find_element_by_id("icm-years-select"))

for yearOption in selectYear.options:
    yearText = yearOption.text
    selectYear.select_by_visible_text(yearText)
    time.sleep(1)

    selectMakes = Select(driver.find_element_by_id("icm-makes-select"))

    for makesOption in selectMakes.options:
        makesText = makesOption.text
        selectMakes.select_by_visible_text(makesText)
        time.sleep(1)
        selectModel = Select(driver.find_element_by_id("icm-models-select"))

        for modelOption in selectModel.options:
            modelText = modelOption.text
            selectModel.select_by_visible_text(modelText)

enter image description here See more