AttributeError: type object 'By' has no attribute 'id' Selenium

AttributeError: type object 'By' has no attribute 'id' Selenium

我正在尝试使用 Selenium 填写下拉表单。我正在通过 ID 尝试 select 下拉元素,但出现此错误:AttributeError: type object 'By' has no attribute 'id' 我得到这个此行的错误:element = driver.find_element(By.id('State'))

from lib2to3.pgen2 import driver
from sre_parse import State
from tkinter.tix import Select
from selenium import webdriver
from selenium.webdriver.support.select import Select
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
import time
# web = webdriver.Chrome(ChromeDriverManager().install())
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
driver.get("https://ssg2021.in/citizenfeedback")

time.sleep(2)

element = driver.find_element(By.id('State'))
drp = Select(element)

drp.select_by_visible_text('Chhattisgarh')
wait=WebDriverWait(driver,30)                                 
driver.get("https://ssg2021.in/citizenfeedback")
element = wait.until(EC.presence_of_element_located((By.ID,"State")))
drp = Select(element)
drp.select_by_visible_text('Chhattisgarh')

只需等待元素出现,然后 select 按文本。

输出:

进口:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC

您可以尝试以下方法

select = Select(driver.find_element_by_id('State'))

# select by visible text
select.select_by_visible_text('Chhattisgarh')