How can I select item from dropdown, when option's element not interactable via selenium/python?

How can I select item from dropdown, when option's element not interactable via selenium/python?

我想 select 下拉列表中的项目,但是要 selected 的选项不可交互,并显示 (Alert: This element is not可通过 selenium(自动化)进行交互,因为它在 UI 中不可见。尝试任何附近的元素。了解更多...) 警报。

这里是Html

<div class="col-md-6">
  <div class="floating-label input_design p-0 mt-3">
      <select class="floating-select" id="coursedd" name="title">
          <option disabled="disabled" selected="selected">Select course</option>
                                            
            <option value="1">Class A CDL</option>
                                            
            <option value="2">Test Course Title B</option>
                                            
            <option value="5">NA</option>
                                            
            <option value="6">NA</option>
                                            
          <option value="7">NA</option>
                                            
        </select>
</div>

我试过这个方法,但没用

select_course = Select(driver.find_element(By.XPATH, "//input[@id='cpassword']"))
select_course.select_by_value('2')

根据您提供的 HTML 这有效:

sel = Select(WebDriverWait(driver, 30).until(EC.visibility_of_element_located((By.ID, "coursedd"))))
by_val = sel.select_by_value('2')
print(sel.first_selected_option.text)

额外导入:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

输出:

Test Course Title B

Process finished with exit code 0