如何在python中模拟onclick事件? [英] How can I simulate onclick event in python?
问题描述
我正在开展一个小项目,我必须向网站提交表格。
但是,该网站使用 onclick
事件来提交表单(使用javascript)。
I am working on a small project where I have to submit a form to a website.
The website is, however, using onclick
event to submit the form (using javascript).
-
如何在python中模拟
onclick
事件?
可以使用哪些模块?我听说过 selenium
和 mechanize
模块。但是,哪个模块可以使用,或者两者兼而有之,哪一个更好?
Which modules can be used? I have heard about selenium
and mechanize
modules. But, which module can be used or in case of both, which one is better?
我是网络新手刮擦和自动化。所以,这将非常有帮助。
I am new to web scraping and automation.So,it would be very helpful.
提前致谢。
推荐答案
在网页上模拟 onclick
事件没有灵丹妙药。它几乎是用例,但这里有一些要点和指导。
There is no silver bullet in simulating onclick
events on a web page. It is pretty much use-case specific, but here are some points and guidelines.
一般来说,有两种方法:
In general, there are two approaches:
- 使用浏览器开发人员工具,打开网络选项卡,点击并查看正在向服务器发送的请求。然后,在Python中模拟此请求,例如,
requests
。 - 使用
selenium
这会启动一个真正的浏览器,在那里您可以找到特定元素,然后点击.click()
方法
- use browser developer tools, open the network tab, make the click and see what request is being sent to the server. Then, simulate this request in Python, with, for example,
requests
. - use
selenium
which would fire up a real browser where you would find the specific element and click via.click()
method
mechanize
不会执行/触发 onclick
功能,因为执行 onclick
需要执行javascript mechanize
做不到。
mechanize
would not execute/trigger the onclick
function, because executing onclick
requires executing javascript which mechanize
cannot do.
这篇关于如何在python中模拟onclick事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!