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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19039789/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 12:37:36  来源:igfitidea点击:

Python script to click a web page button

pythonweb

提问by yuvi

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

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

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. Any thoughts how I can do this?

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

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.
  • 用户在客户端运行脚本
  • 脚本运行并收集静态数据
  • 脚本使用请求将数据作为帖子发送
  • 数据被保存到一个名为“Report”的模型中,一个名为“public”的布尔字段标记为 False
  • 用户切换到 Web 应用程序
  • 用户单击执行 ajax 调用的按钮并获取该报告的编辑表单(通过比较登录用户的用户名,它知道您是发送它的人)
  • 用户添加额外数据并保存
  • 字段“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?

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

回答by Josh Smeaton

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”附加到 URL,您就会得到正确的行为。