【问题标题】:Selenium unable to fetch the select resultsSelenium 无法获取选择结果
【发布时间】:2021-05-15 11:32:27
【问题描述】:

我是 selenium 的新手,并试图从一个站点(代码中的链接)获取 select 标签的值,但每当我尝试获取 select 标签时,它都会返回一个空字段。

我的代码


from selenium import webdriver
from selenium.webdriver.support.ui import Select

driver = webdriver.Chrome('chromedriver',chrome_options=chrome_options)

url = 'https://lifeinsurance.adityabirlacapital.com/about-us/public-disclosure'
driver.get(url)

select = Select(driver.find_element_by_id("select-public-year"))

print([option.text for option in select.options])
# Output: ['']

当我查看页面源代码时,它显示了以下内容:

<select Name="selectPublicYear" class="demo-default select selectized selectPublicYear" id="select-public-year" name="SelectedYear">
  <option value="2020-2021">2020-2021</option>
  <option value="2019-2020">2019-2020</option>
  <option value="2018-2019">2018-2019</option>
  <option value="2017-2018">2017-2018</option>
  <option value="2016-2017">2016-2017</option>
  <option value="2015-2016">2015-2016</option>
  <option value="2014-2015">2014-2015</option>
  <option value="2013-2014">2013-2014</option>
  <option value="2012-2013">2012-2013</option>
  <option value="2011-2012">2011-2012</option>
  <option value="2010-2011">2010-2011</option>
  <option value="2009-2010">2009-2010</option>
  <option value="2008-2009">2008-2009</option>
  <option value="2007-2008">2007-2008</option>
  <option value="2006-2007">2006-2007</option>
  <option value="2005-2006">2005-2006</option>
</select>

任何提示或帮助将不胜感激。

【问题讨论】:

  • 除非您展开列表,否则这些选项可能不会加载到 DOM。因此,请尝试全部单击该选择以展开它,然后再次获取选项。
  • 我该怎么做
  • select.click()?
  • AttributeError: 'Select' object has no attribute 'click' 它给出了以下错误
  • 先试试driver.find_element_by_id("select-public-year").click(),然后再试试select = Select(driver.find_element_by_id("select-public-year"))

标签: python selenium xpath css-selectors webdriverwait


【解决方案1】:

&lt;select&gt; 标记具有 style="display: none;" 属性,因此这不是您想要的元素。


提取文本,例如2020-2021,从所有的&lt;class="option"&gt; 中使用Selenium 和,您必须将WebDriverWait 诱导为visibility_of_all_elements_located(),您可以使用以下任一Locator Strategies:

  • 使用CSS_SELECTOR:

    driver.get('https://lifeinsurance.adityabirlacapital.com/about-us/public-disclosure')
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.selectize-input.items.full.has-options.has-items"))).click()
    print([my_elem.text for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "div.selectize-dropdown-content div.option")))])
    
  • 使用XPATH:

    driver.get('https://lifeinsurance.adityabirlacapital.com/about-us/public-disclosure')
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='selectize-input items full has-options has-items']"))).click()
    print([my_elem.text for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//div[@class='selectize-dropdown-content']//div[contains(@class, 'option')]")))])
    
  • 控制台输出:

    ['2020-2021', '2019-2020', '2018-2019', '2017-2018', '2016-2017', '2015-2016', '2014-2015', '2013-2014', '2012-2013', '2011-2012', '2010-2011', '2009-2010', '2008-2009', '2007-2008', '2006-2007', '2005-2006']
    
  • 注意:您必须添加以下导入:

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

【讨论】:

  • @ShaikhAbuzar 很高兴能为您提供帮助。 Vote up questions and answers 您发现这对未来读者的利益很有帮助。见Why is voting important。
  • 谢谢,工作就像一个魅力。我现在想知道是否要选择一年和一个季度并查看选择结果,然后下载显示的所有文件。你能分享一些关于我应该如何去做的参考吗?
  • @ShaikhAbuzar 很大程度上取决于如何定义下载所有文件的元素。如果它们是静态的,您可以循环单击。但如果它们包含__doposback,您需要稍微调整一下点击策略。
  • 能否请您参考一些关于静态下载方法的好材料。
  • @ShaikhAbuzar 见this讨论。
【解决方案2】:

这似乎是同步问题。我建议使用WebDriverWait() 并等待element_to_be_clickable()

select = Select(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.ID,"select-public-year"))))

print([option.text for option in select.options])

你需要导入下面的库

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

使用 java 测试的快照,因为我的工作机器中没有 python。

【讨论】:

  • TimeoutException: Message: 它给出了以下错误
  • @ShaikhAbuzar :您可能会发布一些其他类似的代码,我用 java 运行过并且它运行良好。请看附件。
  • 请查看我创建的 colab 笔记本的链接:Colab Link
猜你喜欢
  • 2017-09-05
  • 1970-01-01
  • 1970-01-01
  • 2013-04-02
  • 2018-11-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-06
相关资源
最近更新 更多