【问题标题】:Submitting a Form using Selenium in Python在 Python 中使用 Selenium 提交表单
【发布时间】:2016-07-22 07:46:38
【问题描述】:

我需要从this Site 中抓取这些超链接后面的一些数据。但是,这些超链接是javascript function calls,它稍后会使用post 方法提交form。经过一番搜索,selenium 似乎是候选人。所以我的问题是,我应该如何正确地为输入标签设置一个值并提交没有提交按钮的表单。

from selenium import webdriver

url = "http://www.echemportal.org/echemportal/propertysearch/treeselect_input.action?queryID=PROQ3h3n"
driver = webdriver.Firefox()
driver.get(url)
treePath_tag = driver.find_element_by_name("treePath")

在提交表单之前,我需要为标签<input> 赋值。但是,我遇到了一个错误

消息:元素当前不可见,因此可能无法交互 与

treePath_tag.send_keys('/TR.SE00.00/QU.SE.DATA_ENV/QU.SE.ENV_ENVIRONMENT_DATA/QU.SE.EN_MONITORING')

如果以上正确,我想以这种方式提交表格。对吗?

selenium.find_element_by_name("add_form").submit()

以下是网页的来源。

JavaScript 函数

<script type="text/javascript">
    function AddBlock(path){
        document.add_form.treePath.value=path;
        document.add_form.submit();
    }
</script>

表格“add_form”

<form id="addblock_input" name="add_form" action="/echemportal/propertysearch/addblock_input.action" method="post" style="display:none;">
<table class="wwFormTable" style="display:none;"><tr style="display:none;">
  <td colspan="2">
<input type="hidden" name="queryID" value="PROQ3h1w" id="addblock_input_queryID"/>  </td>
</tr>
<tr style="display:none;">
  <td colspan="2">
<input type="hidden" name="treePath" value="" id="addblock_input_treePath"/>  </td>
</tr>
</table></form>

带有javascript调用的div

<div id="querytree">
    <h1>Property Search</h1>
    <h2>Select Query Block Type</h2>
    <p>Select a section for which to define query criteria.</p>
    <div class="queryblocktools"><a href="javascript:document.load_form.submit();"><img style="vertical-align:top;" alt="Load" src="/echemportal/etc/img/load.gif"/>&nbsp;Load Query</a></div>
    <ul class="listexpander">   
    <li>Physical and chemical properties<ul>
        <li><a href="javascript:AddBlock('/TR.SE00.00/QU.SE.DATA_PHYS/QU.SE.PC_MELTING');">Melting point/freezing point</a></li>
        <li><a href="javascript:AddBlock('/TR.SE00.00/QU.SE.DATA_PHYS/QU.SE.PC_BOILING');">Boiling point</a></li>
    </ul>
</div>

【问题讨论】:

  • 你试过thisexecute_script吗?
  • 您是否应该为隐藏字段赋值? 不会在浏览器 GUI 中显示
  • @Aishu 感谢您的评论。

标签: javascript python forms selenium


【解决方案1】:

您正在尝试在页面上不可见的hidden 输入上设置值,这就是发生错误的原因。如果您想在hidden 字段上设置值,请尝试使用execute_script,如下所示:-

treePath_tag = driver.find_element_by_name("treePath")
driver.execute_script('arguments[0].value = arguments[1]', treePath_tag, '/TR.SE00.00/QU.SE.DATA_ENV/QU.SE.ENV_ENVIRONMENT_DATA/QU.SE.EN_MONITORING')

hidden 字段上设置值后,您可以使用以下形式到submit:-

selenium.find_element_by_name("add_form").submit()

希望对你有帮助..:)

【讨论】:

  • 感谢您的建议! arguments[0].value=argument[1]' since I got this error selenium.common.exceptions.WebDriverException 是什么意思:消息:争论未定义`
  • @tao.hong 对不起它的arguments[1]..只是拼写错误..这里 arguments[0] 是您的隐藏元素,arguments[1] 是您要设置的值...: )
  • 不用担心。但我仍然认为它需要定义arguments,因为我得到了这个消息selenium.common.exceptions.WebDriverException: Message: arguements is not defined
  • 再次感谢。但是这一次,它看起来程序被停止了......它打开了firefox然后我的python控制台没有得到任何返回/响应......
  • @tao.hong 有没有异常??你期望得到什么回报??你能解释更多吗??
猜你喜欢
  • 2019-09-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-04
  • 2022-11-02
  • 1970-01-01
  • 2014-06-17
相关资源
最近更新 更多