如何使用seleniumpython[dependent selection]取消多级下拉列表

2021-08-02 18:09:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我想从一个多级选择相关的下拉菜单中提取一些数据,任何人都可以告诉我如何使用selenium选择和单击项目,这样子下拉菜单就可以自动更新了!你知道吗

您可以访问源代码以供参考,也可以运行代码以查看单击后的输出—它从不选择项。你知道吗

我写了好几段代码,这里就是其中之一!你知道吗

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])