How exciting will it be if you can create a repository in Github using your console/command prompt without actually going to Github?
After working for a while on websites, I was looking forward to make the lengthy process of filling up forms in websites automated and that's when I tumbled upon Selenium. You can make your entire Web Browser Automated using Selenium.
In this blog, I am gonna show how I made a simple application to make the process of creating a new repository in Github automated. I am new to this thing and I intend to explore more about it and make some more exciting projects and share with you guys.
How to start building your application?
First you need to install Selenium in your local system. Here's how you can do that:
Once you have completed the above task then you need to install the webdriver from this link. So now let's get into some coding. Open your favorite IDE and create a file and name it confidential.py and in this file you have to declare two variables username and password and assign your github account username and password to the variables respectively, never share this file with anyone, it has some serious consequences. Your file must look something like this:
username = "<Your Github Username>"
password = "<Your Github Password>"
Now you need to create another python file where we are going to declare a class which will have the logic for the functioning of the application. In this file you begin by importing the libraries you need for this application and they are:
from selenium import webdriver
from confidential import username,password
After importing the required libraries you need to create a class, lets name it Bot. In this class we now declare the init function, in which you declare the driver variable. Now the code looks something like this :
from selenium import webdriver
from confidential import username,password
class Bot():
def __init__(self):
self.driver = webdriver.Chrome()
You need a function to automatically login into your Github account using the username and password you declared in the file confidential.py. The code for this function looks something like this :
def login(self):
self.driver.get('https://github.com/')
# clicks on the login button
login = self.driver.find_element_by_xpath('/html/body/div[1]/header/div/div[2]/div[2]/a[1]')
login.click()
# clicks and inputs in the username field
uname = self.driver.find_element_by_xpath('//*[@id="login_field"]')
uname.click()
uname.send_keys(username)
# clicks and inputs in the password field
pwd = self.driver.find_element_by_xpath('//*[@id="password"]')
pwd.click()
pwd.send_keys(password)
# click the login button
button = self.driver.find_element_by_xpath('//*[@id="login"]/form/div[4]/input[12]')
button.click()
Here you have to fetch the fields of the form in the Github sign in page using xpath. To get the xpath, inspect the page and click on the element for which you want the xpath and then right click on the tag and copy the xpath.
Now you need to create another function to create a new repository after sign in. The code for this function looks something like this :
def newRepo(self):
self.driver.get('https://github.com/new')
repo = input("Enter your repository name: ")
description = input("Enter your repository description: ")
# Input repo name
repoName = self.driver.find_element_by_xpath('//*[@id="repository_name"]')
repoName.click()
repoName.send_keys(repo)
# Input repo description
repoDes = self.driver.find_element_by_xpath('//*[@id="repository_description"]')
repoDes.click()
repoDes.send_keys(description)
# Create a public repo
public = self.driver.find_element_by_xpath('//*[@id="repository_visibility_public"]')
public.click()
# select to add a readme file
checkBox = self.driver.find_element_by_xpath('//*[@id="repository_auto_init"]')
checkBox.click()
# create repo
create = self.driver.find_element_by_xpath('//*[@id="new_repository"]/div[4]/button')
create.submit()
This function will help you create a repository automatically.What you have to do is just input the name and description of the repository from the console/command prompt and there, you just created a new repository and that too in a more fast and efficient method.
The final code looks something like this :
from selenium import webdriver
from confidential import username,password
class Bot():
def __init__(self):
self.driver = webdriver.Chrome()
def login(self):
self.driver.get('https://github.com/')
# clicks on the login button
login = self.driver.find_element_by_xpath('/html/body/div[1]/header/div/div[2]/div[2]/a[1]')
login.click()
# clicks and inputs in the username field
uname = self.driver.find_element_by_xpath('//*[@id="login_field"]')
uname.click()
uname.send_keys(username)
# clicks and inputs in the password field
pwd = self.driver.find_element_by_xpath('//*[@id="password"]')
pwd.click()
pwd.send_keys(password)
# click the login button
button = self.driver.find_element_by_xpath('//*[@id="login"]/form/div[4]/input[12]')
button.click()
def newRepo(self):
self.driver.get('https://github.com/new')
repo = input("Enter your repository name: ")
description = input("Enter your repository description: ")
# Input repo name
repoName = self.driver.find_element_by_xpath('//*[@id="repository_name"]')
repoName.click()
repoName.send_keys(repo)
# Input repo description
repoDes = self.driver.find_element_by_xpath('//*[@id="repository_description"]')
repoDes.click()
repoDes.send_keys(description)
# Create a public repo
public = self.driver.find_element_by_xpath('//*[@id="repository_visibility_public"]')
public.click()
# select to add a readme file
checkBox = self.driver.find_element_by_xpath('//*[@id="repository_auto_init"]')
checkBox.click()
# create repo
create = self.driver.find_element_by_xpath('//*[@id="new_repository"]/div[4]/button')
create.submit()
if __name__ == "__main__":
github = Bot()
github.login()
github.newRepo()
So that's my small application which I made using Selenium. Hope you guys liked it, if you did then do comment and also suggestions for future project ideas are welcome. Here is the link to the Video
Top comments (1)
Hi,
Have you tried using curl and github API from command line?
stackoverflow.com/questions/544567...