[英]Submitting a Form using Selenium in Python
我需要從本網站的那些超鏈接中抓取一些數據。 但是,這些超鏈接是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()
以下是來自網頁的資源。
<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 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"/> 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
,如下所示:-
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()
希望能幫助到你..:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.