Python mechanize-提交表单两次

Python mechanize-提交表单两次,python,forms,mechanize,form-submit,Python,Forms,Mechanize,Form Submit,我有这个网站与此表格,我想填写。有许多表格需要提交,在第一个表格中,您必须填写输入标签。当您提交第一个表单时,它会生成另一个表单,其中有一个表显示您刚刚提交的内容,该表单还有一个提交按钮,您必须单击该按钮以确认您的操作。我假设网站使用javascript删除第一个表单,然后生成下一个表单,因为url在该转换过程中不会更改 如何提交/确认下一张表格,即提交第一张表格时生成的表格 用于填写第一张表格的代码 import mechanize b = mechanize.Browser() b.ope

我有这个网站与此表格,我想填写。有许多表格需要提交,在第一个表格中,您必须填写输入标签。当您提交第一个表单时,它会生成另一个表单,其中有一个表显示您刚刚提交的内容,该表单还有一个提交按钮,您必须单击该按钮以确认您的操作。我假设网站使用javascript删除第一个表单,然后生成下一个表单,因为url在该转换过程中不会更改

如何提交/确认下一张表格,即提交第一张表格时生成的表格

用于填写第一张表格的代码

import mechanize

b = mechanize.Browser()
b.open("http://www.website.com/a2b.php")

b.select_form("snd")
b.find_control("t1").value = '200'  # number 1
b.find_control("t2").value = '250'  # number 2
b.find_control("t3").value = '300'  # number 3

b.submit()
第一种形式

<form method="POST" name="snd" action="a2b.php">
 <input name="t1" value="" type="text">
 <input name="t2" value="" type="text">
 <input name="t3" value="" type="text">
 <button type="submit" value="ok" name="s1"></button>
</form>

第二种形式

<form method="post" action="a2b.php">
 <table> 
    Table showing the entered data, entered in previous form
</table>

 <input name="timestamp" value="1445368847" type="hidden">
 <input name="timestamp_checksum" value="JEN8mj" type="hidden">
 <input name="ckey" value="20040" type="hidden">
 <input name="id" value="39" type="hidden">
 <input name="a" value="533374" type="hidden">
 <input name="c" value="3" type="hidden">
 <button type="submit" value="ok" name="s1" id="btn_ok"></button>
</form>

显示输入数据的表格,在以前的表格中输入

Mechanize不能很好地使用javascript,请尝试使用它。它将充当浏览器,包括JS支持。您可以使用等待第二个表单出现

下面是一个如何填写表单的示例(我根据以下内容改编代码):


根据您的需要调整此选项

你能给我们提供真实的网址吗?这样我们就可以复制这个例子了?谢谢。这是一个叫做travian的浏览器游戏,你需要登录。好的,但是你用mechanize打开的URL是什么?谢谢。这是url,感谢您为表单提供url。我注册了该网站/游戏,并试图在浏览器中逐步完成该过程,但没有遇到您提到的第二个表单。第一个表单是登录表单,对吗?第二个表单/屏幕应该是什么?谢谢。看起来是个选择,不过我不太可能让它正常工作。我还需要登录页面,然后才能进入2个表单。此外,第二个表单只是一个确认表单,因此不需要更改任何值。还有一个表单允许您记录浏览器操作,然后重新生成,甚至在以后通过编程对其进行编辑。这样可能更容易理解。
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Firefox()
driver.get("http://www.website.com/a2b.php")

def find_by_xpath(locator):
    element = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.XPATH, locator))
    )

    return element

class FirstFormPage(object):
  def fill_form(self):
    find_by_xpath('//input[@name="t1"]').send_keys('200')
    find_by_xpath('//input[@name="t2"]').send_keys('250')
    find_by_xpath('//input[@name="t3"]').send_keys('300')

    return self # makes it so you can call .submit() after calling this function

  def submit(self):
    find_by_xpath('//input[@value = "ok"]').click()

class SecondFormPage(object):
  def fill_form(self):
    find_by_xpath('//input[@name="timestamp"]').send_keys('1445368847')
    find_by_xpath('//input[@name="timestamp_checksum"]').send_keys('JEN8mj')
    find_by_xpath('//input[@name="ckey"]').send_keys('20040')
    find_by_xpath('//input[@name="id"]').send_keys('39')
    find_by_xpath('//input[@name="a"]').send_keys('533374')
    find_by_xpath('//input[@name="c"]').send_keys('3')

    return self # makes it so you can call .submit() after calling this function

  def submit(self):
    find_by_xpath('//input[@value = "ok"]').click()

FirstFormPage().fill_form().submit()
SecondFormPage().fill_form().submit()
driver.quit() # closes the webbrowser