如何 select 一个选项值,点击另一个菜单然后点击提交

How to select a option value, click in another menu and then hit submit

我的这个页面有两个菜单和一个提交按钮。 我想 select 第一个菜单中的一个选项 (companies),然后 select 第二个菜单中的一个项目 (type) 最后点击提交按钮 (发送)

这是简化的 HTML 页面:

                             <select name="companies" multiple="multiple" id="IDcompanies" style="width:200px;">
    <option value="01">Facebook</option>
    <option value="02">Oracle </option>
    <option value="03">AWS</option>
    <option value="04">Tesla</option>
    </select>
    
    
                             <select name="type"  id="IDtype" style="width:200px;">
    <option value="T1">Type1 </option>
    <option value="T2">Type2 </option>
    <option value="T3">Type3</option>
    </select>
                     

    <input type="submit" name="Button1" value="Send" id="ID_Button1" />

在 python 中,我正在尝试第一部分:点击第一家公司 (Facebook):

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By

PATH = "C:\Program Files (x86)\chromedriver.exe"

driver = webdriver.Chrome(PATH)

driver.get(url)

box = driver.find_element(By.ID, "IDcompanies")

box.select_by_index(0)

但是给我一个错误:AttributeError: 'WebElement' object has no attribute 'select_by_index'

如果有人可以帮助解决这个错误并指导我如何继续点击第一个菜单,然后点击第二个菜单,然后点击提交按钮,我将不胜感激。

为了使用特殊的 Selenium 方法,如 select_by_indexselect_by_valueselect_by_visible_text,您应该定义并初始化特殊的 Selenium Select 对象,如下所示:

companies_select = Select(driver.find_element(By.ID, "IDcompanies"))

companies_select.select_by_index(0)

有关详细信息,请参阅 here