简体   繁体   English

Python Selenium - 下拉菜单

[英]Python Selenium - Drop down menu

EDIT: SOLVED !编辑:已解决!

I just started using selenium and I can't seem to figure out how to choose from the drop down menu.我刚开始使用 selenium,我似乎不知道如何从下拉菜单中进行选择。 I'm trying to choose the Metered Volumes (All) option under the SelectReport but it's giving me an error saying that the SelectReport element does not exist.我试图在SelectReport下选择Metered Volumes (All)选项,但它给我一个错误,指出SelectReport元素不存在。

Here's the HTML of the site :这是网站的 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>

And here's what I tried so far:到目前为止,这是我尝试过的:

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

It would be really helpful if someone can help.如果有人可以提供帮助,那将非常有帮助。 I've been stuck with this issue for a couple of days now.我已经被这个问题困扰了几天了。

To select the <option> with text as Metered Volumes (All) using Selenium you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies :对于 select 使用Selenium将文本作为计量卷(全部)<option>您需要为element_to_be_clickable()引入WebDriverWait并且您可以使用以下任一定位器策略

  • Using CSS_SELECTOR and select_by_visible_text() :使用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)")
  • Using XPATH and select_by_value() :使用XPATHselect_by_value()

     Select(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//select[@name='SelectReport']")))).select_by_value("Market/Reports/PublicSummaryAllReportServlet")
  • Note : You have to add the following imports:注意:您必须添加以下导入:

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

References参考

You can find a couple of relevant discussions in:您可以在以下位置找到几个相关的讨论:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM