How to create GitHub repository using Python Selenium?
Last Updated :
21 Oct, 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 sign in button
signin = driver.find_element_by_xpath(
'//*[@id="login"]/div[4]/form/input[14]')
signin.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 signin button
signin = driver.find_element_by_xpath(
'//*[@id="login"]/div[4]/form/input[14]')
signin.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("Repository created")
create_repo = driver.find_element_by_xpath(
'//*[@id="new_repository"]/div[4]/button')
create_repo.click()
Output:
Similar Reads
How to Add Chrome Extension using Python Selenium IntroductionSelenium is a tool for browser automation that supports multiple browsers for testing. With webdriver we can use multiple languages like Python, C#, and Java to write the code for automation testing. For Adding a Chrome extension in a browser with Selenium WebDriver allows us to automate
5 min read
Get all text of the page using Selenium in Python As we know Selenium is an automation tool through which we can automate browsers by writing some lines of code. It is compatible with all browsers, Operating systems, and also its program can be written in any programming language such as Python, Java, and many more. Selenium provides a convenient A
3 min read
create_web_element driver method - Selenium Python Seleniumâs Python Module is built to perform automated testing with Python. Selenium Python bindings provides a simple API to write functional/acceptance tests using Selenium WebDriver. To open a webpage using Selenium Python, checkout â Navigating links using get method â Selenium Python. Just bein
2 min read
page_source driver method - Selenium Python Seleniumâs Python Module is built to perform automated testing with Python. Selenium Python bindings provides a simple API to write functional/acceptance tests using Selenium WebDriver. To open a webpage using Selenium Python, checkout â Navigating links using get method â Selenium Python. Just bein
2 min read
Web Driver Methods in Selenium Python Seleniumâs Python Module is built to perform automated testing with Python. Selenium Python bindings provides a simple API to write functional/acceptance tests using Selenium WebDriver. To open a webpage using Selenium Python, checkout â Navigating links using get method â Selenium Python. Just bein
5 min read
How to Create a Selenium Maven Project with Eclipse to Open Chrome Browser? Maven is a Yiddish word meaning accumulator of knowledge, Maven is a build automation tool. It is a software project management and comprehension tool that can manage the projectâs build, reporting, and documentation. Selenium is an open-source popular web-based automation tool. The major advantage
3 min read