Skip to content
Related Articles

Related Articles

Improve Article

How to create GitHub repository using Python Selenium?

  • Last Updated : 24 Feb, 2021

Prerequisite: Selenium

Selenium is a powerful tool for controlling web browsers through programs and performing browser automation. It is functional for all browsers, works on all major OS and its scripts are written in various languages i.e Python, Java, C#, etc, we will be working with Python. Selenium Tutorial covers all topics such as – WebDriver, WebElement, Unit Testing with selenium. This Python Selenium Tutorial covers Selenium from basics to advanced and professional uses.

In this article, we will write a python script that will create a GitHub repository using selenium in Python

Steps for creating a GitHub repository:

  • Before creating a repository, we need to login first (Require username and password).
  • After Login, we will go for creating a new repository, and then we require the repository name.
  • After this, three things are left; descriptions, mode(Private or Public), and readme file.

Step-by-step Approach:



Step 1: Import module and create Chrome Object

Python3




# import Module
from selenium import webdriver
  
# Create Chrome Object
driver = webdriver.Chrome(
    'Chrome Driver Path')

Step 2: Create github_repo() with the following arguments to access user GitHub with suitable parameters and perform required tasks.

Python3




def github_repo(user_name, pass_word, 
                repository_name, descriptions=False,
                private=False, readme=False):
      
    # Open github login page
    driver.get('https://github.com/login')
  
    # Username
    username = driver.find_element_by_xpath('//*[@id="login_field"]')
    username.send_keys(user_name)
  
    # Password
    password = driver.find_element_by_xpath('//*[@id="password"]')
    password.send_keys(pass_word)
  
    # Click on sigin button
    sigin = driver.find_element_by_xpath(
        '//*[@id="login"]/div[4]/form/input[14]')
    sigin.click()
  
    # Create new repo.
    new_repo = driver.find_element_by_xpath('//*[@id="repos-container"]/h2/a')
    new_repo.click()
  
    # Enter Repo. name
    repositoryname = driver.find_element_by_xpath('//*[@id="repository_name"]')
    repositoryname.send_keys(repository_name)
  
    # Optional
  
    # Enter Description
    if descriptions:
        description = driver.find_element_by_xpath(
            '//*[@id="repository_description"]')
        description.send_keys(descriptions)
  
    # Private Mode
    if private:
        private = driver.find_element_by_xpath(
            '//*[@id="repository_visibility_private"]')
        private.click()
  
    # Create ReadMe File
    if readme:
        readme = driver.find_element_by_xpath(
            '//*[@id="repository_auto_init"]')
        readme.click()

Step 3: Call the above function with suitable parameters in the driver code to create a GitHub repository.

Python3




github_repo("Enter Usename", "Enter Password"
            "Repository name")

Below is the Implementation:

Python3




# import Module
from selenium import webdriver
  
# Create Chrome Object
driver = webdriver.Chrome('Chrome Driver Path')
  
  
def github_repo(user_name, pass_word, 
                repository_name, descriptions=False,
                private=False, readme=False):
      
    # Open github login page
    driver.get('https://github.com/login')
  
    # Username
    username = driver.find_element_by_xpath('//*[@id="login_field"]')
    username.send_keys(user_name)
  
    # Password
    password = driver.find_element_by_xpath('//*[@id="password"]')
    password.send_keys(pass_word)
  
    # Click on sigin button
    sigin = driver.find_element_by_xpath(
        '//*[@id="login"]/div[4]/form/input[14]')
    sigin.click()
  
    # Create new repo.
    new_repo = driver.find_element_by_xpath('//*[@id="repos-container"]/h2/a')
    new_repo.click()
  
    # Enter Repo. name
    repositoryname = driver.find_element_by_xpath('//*[@id="repository_name"]')
    repositoryname.send_keys(repository_name)
  
    # Optional
  
    # Enter Description
    if descriptions:
        description = driver.find_element_by_xpath(
            '//*[@id="repository_description"]')
        description.send_keys(descriptions)
  
    # Private Mode
    if private:
        private = driver.find_element_by_xpath(
            '//*[@id="repository_visibility_private"]')
        private.click()
  
    # Create ReadMe File
    if readme:
        readme = driver.find_element_by_xpath(
            '//*[@id="repository_auto_init"]')
        readme.click()
  
  
github_repo("Enter Usename", "Enter Password"
            "Repository name")
  
print("Respositry created")
  
create_repo = driver.find_element_by_xpath(
    '//*[@id="new_repository"]/div[4]/button')
  
create_repo.click()

Output:

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :