用于单击网页按钮的 Python 脚本 [英] Python script to click a web page button

查看:64
本文介绍了用于单击网页按钮的 Python 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 python 脚本,它使用请求库将数据发送到 django 应用程序.然后用户切换到网页并单击一个按钮,该按钮获取一个编辑表单以添加一些附加信息

我希望在请求收到状态代码 200 后立即切换到网页并自动单击按钮,而不是用户每次都手动执行.

我考虑过使用 Selenium,但这似乎有点矫枉过正.任何想法我怎么能做到这一点?

编辑

目前的流程有点像这样:

  • 用户在客户端运行脚本
  • 脚本运行并收集静态数据
  • 脚本使用请求将数据作为帖子发送
  • 将数据保存到名为Report"的模型中,将名为public"的布尔字段标记为 False
  • 用户切换到网络应用
  • 用户单击执行 ajax 调用的按钮并获取该报告的编辑表单(通过比较登录用户的用户名,它知道您是发送该报告的人)
  • 用户添加额外数据并保存
  • 公开"字段更改为 True,每个人都可以看到报告.

大部分工作正常,我只是想让脚本自动切换到网页并单击按钮,而不是手动完成.我知道这有点令人费解,但我希望它能更好地解释事情

另外,我使用 Windows 和 Chrome 作为我的网络浏览器

第二次编辑

所以我构建了一个小演示来玩.我创建了一个名为test.html"的文件,如下所示:

<头><title>测试</title><script type='javascript/text' src='jquery.js' ></script><身体><div class="容器"><button id="button1">假的</button><button id="button2">假的</button><button id="button3">真正的</button>

<脚本>$(函数(){$('#button3').on('click', function() {alert('你找到了!');});});</html>

如您所见,运行脚本的唯一方法是单击真实"按钮.现在我编写了一个 python 脚本,将它显示在屏幕上:

导入win32gui类窗口(对象):def __init__(self, handle):断言句柄 != 0self.handle = 句柄@类方法def from_title(cls, title):handle = win32gui.FindWindow(title, None) 或 win32gui.FindWindow(None, title)返回 cls(句柄)chrome = Window.from_title('测试 - 谷歌浏览器')win32gui.SetForegroundWindow(chrome.handle)win32gui.SetFocus(chrome.handle)

现在我如何让它复制用户完成的按钮点击?可能有一种方法可以使用坐标以图形方式进行操作,但这是唯一的方法吗?您如何确保按钮始终位于同一位置?还有比使用按钮更好的方法吗?

解决方案

那么您想要做的就是在页面打开时自动单击一个按钮?我假设您只想在某些情况下自动点击,因此将 javascript/jquery 隐藏在条件后面.

def your_view(request):# 你的标准处理auto_click = request.GET.get('auto_click', False)如果 auto_click 不是 False:auto_click = 真render('yourtemplate.html', {'auto_click': auto_click })

然后,在您的模板中:

{% if auto_click %}<脚本>$(document).ready(function() {$('你的按钮').click();});{% 万一 %}

当用户打开网页时,只需将?auto_click=1"附加到 URL,您就会得到正确的行为.

I have a python script that sends data to a django app using the requests library. Then the users switch to the web page and click a button which fetches an edit form to add some additional info

I want that immediately after requests recieves a status code 200 it will switch to the web page and click the button automatically, instead of the users doing it manually each time.

I looked into using Selenium but it seems like an overkill. Any thoughts how I can do this?

edit

The current process looks a little like this:

  • user runs script on client side
  • script runs and collects static data
  • script sends data as post using requests
  • the data is saved to a model called "Report", with a boolean field called "public" marked as False
  • The user switches to the web app
  • the user clicks a button that does an ajax call and fetches an edit form for that report (it knows you're the one who sent it by comparing the username of the logged in user)
  • the user adds additional data and saves
  • the field "public" changes to True and everyone can see the report.

Most of this is working, I just want the script to automatically switch to the web page and click the button instead of it being done manually. I know this is a little convoluted but I hope it explains things better

Also I'm using Windows and Chrome as my web browser

Second Edit

So I built a little demo to play around with. I created a file named 'test.html' which looks like this:

<html>
    <head>
        <title>Test</title>
        <script type='javascript/text' src='jquery.js' ></script>
    </head>
<body>
    <div class="container">
        <button id="button1"> Fake </button>
        <button id="button2"> Fake </button>
        <button id="button3"> Real </button>
    </div>


<script>
$(function() {
    $('#button3').on('click', function() {
          alert('you found it!');
    });
});

</script>

</body>
</html>

As you can see, the only way to run the script is to click on the "real" button. Now I have written a python script which brings it up into the screen:

import win32gui

class Window(object):
    def __init__(self, handle):
        assert handle != 0
        self.handle = handle

    @classmethod
    def from_title(cls, title):
        handle = win32gui.FindWindow(title, None) or win32gui.FindWindow(None, title)
        return cls(handle)


chrome = Window.from_title('Test - Google Chrome')

win32gui.SetForegroundWindow(chrome.handle)
win32gui.SetFocus(chrome.handle)

Now how do I get it to replicate a button click done by the user? There probably is a way to do it graphically using coordinates but is that the only way? And how do you assure the button will always be at the same spot? Is there a better way than to use a button?

解决方案

So all you want to do is automatically click a button when the page opens? I'm assuming you only want to auto-click in certain circumstances, so hide the javascript/jquery behind a conditional.

def your_view(request):
    # your standard processing
    auto_click = request.GET.get('auto_click', False)
    if auto_click is not False:
        auto_click = True
    render('yourtemplate.html', {'auto_click': auto_click })

Then, in your template:

{% if auto_click %}
<script>
$(document).ready(function() {
    $('yourbutton').click();
});
</script>
{% endif %}

When the user opens up the webpage, just append "?auto_click=1" to the URL, and you'll get the correct behaviour.

这篇关于用于单击网页按钮的 Python 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆