我有這個頁面,有兩個選單和一個提交按鈕。我想在第一個選單(公司)中選擇一個選項,然后在第二個選單中選擇一個專案(型別),最后點擊提交按鈕(發送)
這是簡化的 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'
如果有人可以幫助解決此錯誤并指導我如何繼續單擊第一個選單,然后單擊第二個選單,然后單擊提交按鈕,我將不勝感激。
uj5u.com熱心網友回復:
為了使用特殊的 Selenium 方法select_by_index,你應該定義和初始化特殊的 Seleniumselect_by_value物件,如下所示:select_by_visible_textSelect
companies_select = Select(driver.find_element(By.ID, "IDcompanies"))
companies_select.select_by_index(0)
有關更多詳細資訊,請參見此處
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/442316.html
