简体   繁体   中英

Python mechanize - Submit form twice

I have this website with this form that i would like to fill out. There is to forms that needs to be submitted, in the first form you've to fill out the inputs tags. When you submit that first form, it generate another form with a table showing what you just submitted, this form also have a submit button that you've to click to confirm you action. I would assume that the website uses javascript to remove the first form and then generate the next, cause the url doesn't change in that transition.

How do I submit/confirm the next form, that is generated by when the first form is submitted?

Code used to fill out the first form.

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

First form

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

Second 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 doesn't play well with javascript, try to use Selenium Webdriver instead. It will act as browser, including JS support. You could use Waits to wait for second form to appear.

Here is an example how you could fill your forms (I adapted the code from this answer ):

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

Adapt this to your needs!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM