Created
February 6, 2016 12:55
-
-
Save gh640/695c9bdfbf7587d08c36 to your computer and use it in GitHub Desktop.
Drupal 7 でサイトに新しいユーザを登録する Selenium - Python スクリプト
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| coding: utf-8 | |
| """ | |
| Drupal サイトでユーザを登録するためのスクリプト | |
| 必要なもの | |
| - selenium: `pip install selenium` | |
| """ | |
| import sys | |
| from selenium import webdriver | |
| from selenium.webdriver.common.keys import Keys | |
| # 定数 | |
| URL_BASE = 'ここに対象サイトのフロントページ URL を入力します' | |
| PATH_REGISTER = URL_BASE + '/user/register' | |
| USER_NAME = 'hayato{}' | |
| USER_PASSWORD = 'hayato_password_{}' | |
| USER_ADDRESS = 'mygmail+{}@gmail.com' | |
| def register_new_user(number, browser): | |
| """登録ページを開いて登録する | |
| number: str | |
| browser: selenuim.webdriver | |
| """ | |
| user_name = USER_NAME.format(number) | |
| user_password = USER_PASSWORD.format(number) | |
| user_address = USER_ADDRESS.format(number) | |
| print('Creating a user: {}'.format(user_name)) | |
| browser.get(PATH_REGISTER) | |
| id_and_texts = { | |
| 'edit-name': user_name, | |
| 'edit-mail': user_address, | |
| 'edit-pass-pass1': user_password, | |
| 'edit-pass-pass2': user_password, | |
| } | |
| for id, text in id_and_texts.items(): | |
| elem = browser.find_element_by_id(id) | |
| elem.send_keys(text) | |
| elem = browser.find_element_by_id('edit-submit') | |
| elem.click() | |
| def get_argv(): | |
| """コマンドライン引数の数をチェックして末尾のものを取得する | |
| """ | |
| argv = sys.argv | |
| if len(argv) != 2: | |
| print('Usage: python this_script_name.py [additional_user_name]') | |
| exit() | |
| return argv[-1] | |
| def check_url_base(): | |
| """BASE_URL をチェックする | |
| セットされていなければ終了する | |
| """ | |
| if not URL_BASE: | |
| print('URL_BASE is empty. Please input this before running.') | |
| exit() | |
| if __name__ == '__main__': | |
| # BASE_URL をチェックする | |
| check_url_base() | |
| # コマンドラインからユーザ名の一部を取得する | |
| additional_user_name = get_argv() | |
| # ブラウザを開いてユーザを登録する | |
| browser = webdriver.Firefox() | |
| register_new_user(additional_user_name, browser) | |
| browser.quit() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment