gpt4 book ai didi

Python mechanize - 提交表单两次

转载 作者:行者123 更新时间:2023-11-28 19:16:13 25 4
gpt4 key购买 nike

我有这个网站和我想填写的表格。有需要提交的表格,在第一个表格中,您必须填写输入标签。当您提交第一个表单时,它会生成另一个表单,其中包含一个显示您刚刚提交的内容的表格,该表单还有一个提交按钮,您必须单击该按钮以确认您的操作。我假设该网站使用 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 配合使用,请尝试使用 Selenium Webdriver反而。它将充当浏览器,包括 JS 支持。你可以使用 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

根据您的需要进行调整!

关于Python mechanize - 提交表单两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33245259/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com