javascript - 在 Python 中使用 Selenium 提交表单

标签 javascript python forms selenium

我需要从 this Site 的超链接后面抓取一些数据.但是,这些超链接是 javascript function calls ,稍后提交 form使用 post方法。经过一番搜索,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> .但是,我得到一个错误

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

如果以上是正确的,我想以这种方式提交表格。是否正确?

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>

表单“添加表单”

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

div 与 javascript 调用

<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 字段上设置值,请尝试使用 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')

隐藏字段上设置值后,您可以使用以下方法提交表单:-

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

希望对您有所帮助..:)

关于javascript - 在 Python 中使用 Selenium 提交表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38520970/

相关文章:

javascript - 为什么宽度为 : 0 doesn't work

具有两个键和多个值的文件中的 Python 字典

html - 为响应式布局对齐表单域

python - 如何使用PyAutoGUI检测RGB值

forms - symfony2 通过服务来形成主题

javascript - 比较两个表单电子邮件字段的值

javascript - jQuery AJAX 自动刷新值

javascript - Mongoose : Dynamically load different schema under one collection

javascript - 如何在 JavaScript 中记录用户提交到表单的信息

python - Tensorflow Deeplab 图像颜色图去除混淆