【问题标题】:Can't find matching form找不到匹配的表格
【发布时间】:2014-08-25 15:21:02
【问题描述】:

我正在尝试使用 Python 的 mechanize 模块来提交表单值并下载后续文件。但是,我不断收到错误消息,提示脚本找不到表单。

我使用的网站是here

我正在尝试按 County = 'Linn' 进行选择。

下面是我用来选择表单的脚本...

import mechanize

url = 'https://ccmis.dhs.state.ia.us/clientportal/providersearch.aspx'

br = mechanize.Browser()
br.open(url)
br.select_form(name="ctl00$MainContent$ddlSearchByLocationCounty")

我不断收到错误消息,提示没有具有匹配名称的表单。当我使用开发人员工具时,这是显示的名称。下面是 HTML 的 sn-p...

<select name="ctl00$MainContent$ddlSearchByLocationCounty" id="ctl00_MainContent_ddlSearchByLocationCounty" style="width:150px;">
    <option value="">Select County</option>
    <option value="Adair">Adair</option>
    <option value="Adams">Adams</option>
    <option value="Allamakee">Allamakee</option>

【问题讨论】:

    标签: python forms mechanize


    【解决方案1】:

    您需要先按名称、ID 等选择表单,然后才能选择输入并设置其值。这是将国家/地区设置为 Linn 的更新代码。我建议查看 http://www.pythonforbeginners.com/cheatsheet/python-mechanize-cheat-sheet 的备忘单。

    import mechanize
    
    url = 'https://ccmis.dhs.state.ia.us/clientportal/providersearch.aspx'
    
    br = mechanize.Browser()
    br.open(url)
    br.select_form(name="aspnetForm")
    
    country = br.form.find_control("ctl00$MainContent$ddlSearchByLocationCounty")
    country.value = ['Linn']
    print country.value
    

    【讨论】:

    • 感谢为我指明正确的方向!几个后续问题。 1) 你在哪里找到表单名称是“aspnetForm”? 2) 将值设置为“Linn”后,底部会有一个“搜索”按钮,我想在此进入下一个屏幕。我假设我需要使用br.click_link/follow_link 才能进入下一个屏幕?
    • 1) 我在网站上启动了 Google Chrome 开发者工具 (F12) 并搜索了 &lt;form&gt; 标签,它返回了 &lt;form name="aspnetForm"&gt;。 2) 搜索按钮是输入类型按钮而不是链接。由于有type="submit"、Search和Clear两个按钮,调用submit()时需要额外传递参数:br.submit(name='ctl00$MainContent$btnSearch', label='Search')
    • 我相信我现在已经正确设置了脚本。我现在想知道提交搜索后如何在以下屏幕上下载报告?是否需要放置一个新的 URL 或者它是如何工作的?表格仍然被称为相同的。 “下载报告”控件有一个 ID,但没有名称。我想要的值是“2”......这是下载到Excel。
    猜你喜欢
    • 2020-08-16
    • 1970-01-01
    • 2020-09-29
    • 1970-01-01
    • 2020-09-27
    • 2021-09-04
    • 2018-12-16
    • 2017-10-28
    • 2015-11-13
    相关资源
    最近更新 更多