2.2 Selenium and Pydriver

2.2 Selenium和Pydriver

Before we dive into the code, we need to understand a few basics.

在我们埋头于代码之前,我们需要理解一些基础知识。

For our hacks, we need to automate our web browser, so that our script can click on buttons, fill in forms, etc. There are many ways to do this, but the best is the Selenium Web driver⁴.

为了进行我们的攻击,我们需要自动化我们的web浏览器,这样我们的脚本可以点击按钮,填写表单等等。有很多方法可以做到这一点,但是最好的是Selenium Web driver⁴

You don’t need to install it, as it is included in the virtual machine I gave you. Now Selenium automates the whole browser, so normally, you can see a new Firefox instance opening and buttons being clicked. However, we will be driving the browser headless (which means without a GUI), for two reasons:

你不需要安装它,因为它包含在我给你的虚拟机内。现在Selenium将整个浏览器自动化,所以通常情况下,你可以看到一个新的Firefox实例打开和按钮被单击。然而,我们将在无GUI的情况下使用浏览器),原因有二:

1 Last few months, the latest Firefox has not been working well with Selenium. Firefox is the default browser for Selenium, and while you can install others, the process isn’t easy. The Firefox team are aware of these issues, but I don’t know when they will be solved.

  1. 最近几个月,最新的Firefox和Selenium运行的并不融洽。Firefox是Selenium的默认浏览器,虽然你可以安装其他的浏览器,但是这个过程并不容易。Firefox团队已经意识到了这些问题,但我不知道这些问题什么时候会被解决。

2 We are running the code in a virtual machine, and VMs don’t support GUIs well. There are things like X-forwarding(X-转发) which I won’t go into here, as they don’t always work on all systems.

  1. 我们正在一台虚拟机里运行代码,而且虚拟机对GUI的支持不太好。有些东西,比如X-forwarding,我不会在这里讲,因为它们并不总是在所有的系统上工作。

For these reasons, we will drive our system headless. You won’t see a GUI browser opening, but that doesn’t matter, as we will print the final results on the screen. Besides, if you are running the code on a server (which is very likely), you will not have access to a GUI anyway.

由于以上原因,我们将会在无GUI的条件下运行我们的系统。你不会看到GUI浏览器打开,但这并不重要,因为我们将在屏幕上打印最终结果。此外,如果您在服务器上运行代码(这很有可能),那么您将不需要访问GUI。

Now, Firefox is a graphical program. So how can you run it headless ?There are multiple ways, but the one we will be using is a Python library called pyvirtualdisplay. This creates a “virtual” monitor for Firefox to run in.

现在,Firefox是一个图形化的程序。那么如何在没有图形界面的情况下运行它?有多种方式,但我们将要使用的是一个名为pyvirtualdisplay的Python库。这将创建一个“虚拟”监视器以在其中运行Firefox。

Okay, let’s start looking at our code, and our first hack.

好啦,让我们开始看代码,开始我们的第一次攻击吧。

results matching ""

    No results matching ""