在Python中使用Selenium提交表单 [英] Submitting a Form using Selenium in Python

查看:698
本文介绍了在Python中使用Selenium提交表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从本网站的那些超链接后面抓取一些数据.但是,这些超链接是javascript function calls,该超​​链接随后使用post方法提交form.经过一番搜索,selenium似乎是候选者.所以我的问题是,我应该如何为输入标签正确设置一个值,然后提交不提交按钮的表单.

I need to scrape some data behind those hyperlinks from this Site. However, those hyperlinks are javascript function calls, which later submits a form using post method. After some search, selenium seems to be a candidate. So my question is that how should I properly set a value to an input tag and submit the form which does not a submit a button.

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>赋值.但是,我遇到了错误

Before submitting the form, I need to assign value to tag <input>. However, I got an error

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

Message: Element is not currently visible and so may not be interacted with

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

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

IF above is correct, I would like to submit form this way. Is it correct?

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

以下是网页上的来源.

<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>

推荐答案

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

You are trying to set value on hidden input is which not visible on the page, that's why error has occurred. If you want to set value on hidden field try using execute_script as below :-

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:-

After setting value on hidden field you can use following to submit the form :-

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

希望它会有所帮助..:)

Hope it helps..:)

这篇关于在Python中使用Selenium提交表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆