简体   繁体   English

用于单击网页按钮的Python脚本

[英]Python script to click a web page button

I have a python script that sends data to a django app using the requests library. 我有一个python脚本,使用请求库将数据发送到django应用程序。 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. 我希望在请求收到状态代码200后立即切换到网页并自动点击按钮,而不是用户每次手动执行。

I looked into using Selenium but it seems like an overkill. 我研究过使用Selenium,但这看起来像是一种矫枉过正。 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 脚本使用请求将数据作为post发送
  • the data is saved to a model called "Report", with a boolean field called "public" marked as False 数据被保存到名为“Report”的模型中,名为“public”的布尔字段标记为False
  • The user switches to the web app 用户切换到Web应用程序
  • 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) 用户单击执行ajax调用的按钮并获取该报告的编辑表单(它知道您是通过比较登录用户的用户名发送它的人)
  • the user adds additional data and saves 用户添加其他数据并保存
  • the field "public" changes to True and everyone can see the report. 字段“public”更改为True,每个人都可以看到该报告。

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 我也使用Windows和Chrome作为我的网络浏览器

Second Edit 第二次编辑

So I built a little demo to play around with. 所以我构建了一个小演示来玩。 I created a file named 'test.html' which looks like this: 我创建了一个名为'test.html'的文件,如下所示:

<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: 现在我编写了一个python脚本,将其显示在屏幕上:

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. 我假设你只想在某些情况下自动点击,所以隐藏条件后面的javascript / jquery。

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. 当用户打开网页时,只需在网址上附加“?auto_click = 1”,您就会获得正确的行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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