Writing every week about programming, new technology and books.
Fill credit card info in less then 10 lines of code with this Python script
For this tutorial only thing, you need to have installed Python on your PC/Laptop. If you don't have that I made a simple tutorial about Python installation guide.
Let's make front-end first
First, we are going to make a simple HTML page as our testing ground, to make sure that our code runs correctly.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <label>Full name on credit card</label> <input type="text" name="fullname"/><br/><br/> <label>Credit card number</label> <input type="text" name="ccn"/><br/><br/> <label>Expiration Month</label> <input type="text" name="em"/><br/><br/> <label>Expiration Year</label> <input type="text" name="ey"/><br/><br/> <label>CVV</label> <input type="text" name="cvv"/><br/><br/> </body> </html>
Let's make that magical script
First, we need to create Python script ( creditcard.py in my example ) and import some python packages:
import pyautogui import time
We need a Pyautogui package to let Python control our keyboard and mouse, and time package to pause a script if we need to.
Next thing on the list is adding this line of code:
Here, we are using the time package we just imported and told Python to wait for 3 seconds before executes the first line of code. These three seconds should be enough for us to click and focus on a window where our script should be executed.
When running these scripts I like to split my screen into two parts, Left is for browser window where code is going to executed and on right is terminal.
The next step is to make the array of all the necessary information.
data = [ "Nikola Ciganovic", "5500 0000 0000 0004", "January", "2021", "111" ]
Now, when we have data only thing left to do to insert this data in our text boxes. Here is how to do that:
for key in range(len(data)): pyautogui.typewrite('\t') #press Tab on keyboard pyautogui.typewrite(data[key]) # type element of data array on keyboard
This is simple for loop that for each element in arrays types Tab on a keyboard ( which will focus on text box we want to insert information ) and then type information.
And that it! This is how simple can be python scripts. Everything we just wrote can fit in less than 10 lines of code.
import pyautogui import time time.sleep(3) data = [ "Nikola Ciganovic","5500 0000 0000 0004","January","2021", "111" ] for key in range(len(data)): pyautogui.typewrite('\t') pyautogui.typewrite(data[key])
Thank you for reading this tutorial, I hope you learned something new!