Python 机械化 - 提交表单两次
Python mechanize - Submit form twice
我有这个网站,我想填写这张表格。有需要提交的表格,在第一个表格中,您必须填写输入标签。当您提交第一个表单时,它会生成另一个带有 table 的表单,显示您刚刚提交的内容,此表单还有一个提交按钮,您必须单击该按钮以确认您的操作。我假设该网站使用 javascript 删除第一个表单然后生成下一个,因为 url 在该转换中没有改变。
如何submit/confirm提交第一个表单时生成的下一个表单?
用于填写第一个表格的代码。
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 配合不佳,请尝试使用Selenium Webdriver instead. It will act as browser, including JS support. You could use Waits 等待第二形态出现。
这里是一个示例如何填写表格(我改编了 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
根据您的需要进行调整!
我有这个网站,我想填写这张表格。有需要提交的表格,在第一个表格中,您必须填写输入标签。当您提交第一个表单时,它会生成另一个带有 table 的表单,显示您刚刚提交的内容,此表单还有一个提交按钮,您必须单击该按钮以确认您的操作。我假设该网站使用 javascript 删除第一个表单然后生成下一个,因为 url 在该转换中没有改变。
如何submit/confirm提交第一个表单时生成的下一个表单?
用于填写第一个表格的代码。
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 配合不佳,请尝试使用Selenium Webdriver instead. It will act as browser, including JS support. You could use Waits 等待第二形态出现。
这里是一个示例如何填写表格(我改编了 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
根据您的需要进行调整!