簡體   English   中英

Python Selenium - 下拉菜單

[英]Python Selenium - Drop down menu

編輯:已解決!

我剛開始使用 selenium,我似乎不知道如何從下拉菜單中進行選擇。 我試圖在SelectReport下選擇Metered Volumes (All)選項,但它給我一個錯誤,指出SelectReport元素不存在。

這是網站的 HTML:

 <select name="SelectReport" size="1" onchange="populateReportType(document.reportForm, getCurrReportObj())"> <option selected="" value="NO LINK"> Select a Report </option> <option value="NO LINK"> </option> <option value="NO LINK">SETTLEMENT</option> <option value="Market/Reports/PublicSummaryAllReportServlet">--- Metered Volumes (All)</option> <option value="Market/Reports/DdsPaymentSummaryReportServlet">--- DDS Payment Summary</option> <option value="Market/Reports/DdsChargeSummaryReportServlet">--- DDS...... </select>

到目前為止,這是我嘗試過的:

select = Select(self.driver.find_element_by_name("SelectReport"))
select.select_by_value("Market/Reports/PublicSummaryAllReportServlet")

如果有人可以提供幫助,那將非常有幫助。 我已經被這個問題困擾了幾天了。

對於 select 使用Selenium將文本作為計量卷(全部)<option>您需要為element_to_be_clickable()引入WebDriverWait並且您可以使用以下任一定位器策略

  • 使用CSS_SELECTORselect_by_visible_text()

     # presuming the actual option text is "Metered Volumes (All)" Select(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "select[name='SelectReport']")))).select_by_visible_text("Metered Volumes (All)")
  • 使用XPATHselect_by_value()

     Select(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//select[@name='SelectReport']")))).select_by_value("Market/Reports/PublicSummaryAllReportServlet")
  • 注意:您必須添加以下導入:

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

參考

您可以在以下位置找到幾個相關的討論:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM