情况一:select标签
from selenium.webdriver.support.select import Select #引入Select 类 Select 赋值的三种方法: select_ele = driver.find_element(By.CSS_SELECTOR,"#all_menu_list")#先找到select框 select = Select(select_ele) #将“select_ele”实例化 select.select_by_value("category.php?act=list") #通过value去赋值 select.select_by_index(0) #通过索引赋值 select.select_by_visible_text("测试") #通过可见的文本赋值
情况二:非select标签
adder_one_locator = (By.XPATH,"//*[@id=\"app\"]/div/div[3]/div[2]/div/div[3]/form/div[1]/div/div[1]/div/input")#定位选择框 adder_locator = (By.CSS_SELECTOR,"body > div.el-select-dropdown.el-popper > div.el-scrollbar > div.el-select-dropdown__wrap.el-scrollbar__wrap > ul")#定位选择下拉框
adder_one_select_locator = (By.CSS_SELECTOR,"body > div.el-select-dropdown.el-popper > div.el-scrollbar > div.el-select-dropdown__wrap.el-scrollbar__wrap > ul > li:nth-child(1) > span") #选择首页
def click_one_adder(self): """ 点击位置的第一个选择框 :return: """ self.find_element(self.adder_one_locator).click() def click_one_adder_select(self): """ 在位置的第一个选择框中选择相应的信息 :return: """ a = self.find_element(self.adder_locator) a.find_element(*self.adder_one_select_locator).click()