如何使用Python Selenium创建 GitHub 存储库?
先决条件:Selenium
Selenium是一个强大的工具,用于通过程序控制 Web 浏览器并执行浏览器自动化。它适用于所有浏览器,适用于所有主要操作系统,其脚本是用各种语言编写的,例如Python、 Java、C# 等,我们将使用Python。 Selenium教程涵盖所有主题,例如 – WebDriver、WebElement、使用selenium单元测试。这个Python Selenium教程涵盖了从基础到高级和专业用途的Selenium 。
在本文中,我们将编写一个Python脚本,该脚本将在Python使用selenium创建一个 GitHub 存储库
创建 GitHub 存储库的步骤:
- 在创建存储库之前,我们需要先登录(需要用户名和密码)。
- 登录后,我们将创建一个新的存储库,然后我们需要存储库名称。
- 在这之后,剩下三件事;描述、模式(私人或公共)和自述文件。
循序渐进的方法:
第 1 步:导入模块并创建 Chrome 对象
Python3
# import Module
from selenium import webdriver
# Create Chrome Object
driver = webdriver.Chrome(
'Chrome Driver Path')
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()
Python3
github_repo("Enter Usename", "Enter Password",
"Repository name")
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()
步骤 2:使用以下参数创建github_repo()以使用合适的参数访问用户 GitHub 并执行所需的任务。
蟒蛇3
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存储库。
蟒蛇3
github_repo("Enter Usename", "Enter Password",
"Repository name")
下面是实现:
蟒蛇3
# 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()
输出: