Selenium automates traversal and selects each element in the select drop-down box

When we want to traverse all the elements of the select drop-down box, at this time, we can first obtain the length of the drop-down box, and then use the for loop to select one by one, using len(Select(driver.find_element(By.ID,"dj" )).options) method is used to count the number of drop-down box elements, and then add a for loop to traverse one by one. The specific method is as follows:

import time
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
djjgSelList=EGTconfigFile.driver.find_element(By.ID,"dj")
print(len(Select(djjgSelList).options))
number=1
EGTconfigFile.driver.find_element(By.ID,"keepBtn").click()#点击保存
for number in range(1,len(Select(djjgSelList).options)):
EGTconfigFile.driver.find_element(By.XPATH,"/html/body/div[2]/div/div/div[2]/form/div[3]/div[2]/input[1]").click()#点击新增
    Select(djjgSelList).select_by_index(number)
    EGTconfigFile.driver.find_element(By.ID, "keepBtn").click()  # 点击保存
    time.sleep(2)

Remark:

During the debugging process, I encountered an error and tried to use len(), resulting in the error "TypeError: 'int' object is not iterable

I added a range() method without reporting an error:

range(1,len(Select(djjgSelList).options))

Guess you like

Origin blog.csdn.net/u012388338/article/details/130642154