# coding=UTF-8
import os

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import random
import time
import utils

def main():
    driver_path = os.path.dirname(os.path.abspath(__file__)) + "/chromedriver"
    print driver_path
    driver = webdriver.Chrome(driver_path)
    driver.get("https://tw.event.beanfun.com/mabinogi/e20170511/index.aspx")
    print driver.title
    user = utils.load_account("account.json")

    dice_game(driver, user)

def login(driver, user):
    # Login elements is in iframe, should switch to it
    login_iframe = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.ID, "ifmForm1"))
    )
    driver.switch_to_frame(login_iframe)
    login_btn = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.ID, "btn_login"))
    )
    account = driver.find_element_by_id("t_AccountID")
    account.send_keys(user["account"])
    password = driver.find_element_by_id("t_Password")
    password.send_keys(user["password"])
    login_btn.click()

    # Choose game account
    game_account_selector = driver.find_element_by_id("ddl_service_account")
    for option in game_account_selector.find_elements_by_tag_name('option'):
        if option.text == user["game_account"]:
            option.click()
            driver.find_element_by_id("btn_send_service_account").click()
            break

def dice_game(driver, user):
    # Find dice game
    dice_entry = driver.execute_script("return $('a[href=\"Dice.aspx\"]')[0]")
    dice_entry.click()

    # If not logon
    try:
        login(driver, user)

        while i_have_coin_haha(driver):
            # Start to dice
            go_dice = WebDriverWait(driver, 10).until(
                EC.presence_of_element_located((By.ID, "btnGoDice"))
            )
            time.sleep(random.randint(2, 5))
            driver.execute_script("arguments[0].click()", go_dice)
            # Continue game
            continue_game = WebDriverWait(driver, 10).until(
                EC.presence_of_element_located((By.XPATH, "//a[@class='btn_B']/input[@id='btnGoDice']"))
            )
            time.sleep(random.randint(2, 5))
            driver.execute_script("arguments[0].click()", continue_game)
        print "No coin."

    finally:
        print "Game over."

def i_have_coin_haha(driver):
    coin_field = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.CLASS_NAME, "coin"))
    )
    coin = coin_field.text.encode('utf-8').split("剩餘代幣：")[1]
    return int(coin) > 0

if __name__ == '__main__':
    main()
@@ -17,16 +17,26 @@
 mport os
+, argparse
 %0A%0Afrom s
@@ -269,17 +269,103 @@
 ls%0A%0A
-def main(
+MABINOGI_URL = %22https://tw.event.beanfun.com/mabinogi/e20170511/index.aspx%22%0A%0Adef main(game_type
 ):%0A 
@@ -450,26 +450,79 @@
     
-print driver_path%0A
+users = utils.load_account(%22account.json%22)%0A%0A    for user in users:%0A    
     
@@ -534,24 +534,18 @@
 r = 
-web
+get_
 driver
-.Chrome
 (dri
@@ -562,151 +562,489 @@
     
-driver.get(%22https://tw.event.beanfun.com/mabinogi/e20170511/index.aspx%22)
+    start_new_session(driver, user, game_type)%0A        stop_session(driver)%0A%0Adef start_new_session(driver, user, game_type):%0A    driver.get(MABINOGI_URL)%0A    game.get(game_type, login)(driver, user)%0A%0Adef stop_session(driver):%0A    driver.close()%0A%0Adef get_driver(driver_path):
 %0A    
-print driver.title%0A    user = utils.load_account(%22account.json
+driver = webdriver.Chrome(driver_path)%0A    return driver%0A%0Adef login(driver, user):%0A    # Use dice game as login entry page%0A    dice_entry = driver.execute_script(%22return $('a%5Bhref=%5C%22Dice.aspx%5C%22%5D')%5B0%5D
 %22)%0A
-%0A
     
@@ -1048,33 +1048,78 @@
    dice_
-game(driver, user
+entry.click()%0A    login_routine(driver, user)%0A    time.sleep(3
 )%0A%0Adef l
@@ -1114,32 +1114,40 @@
 ep(3)%0A%0Adef login
+_routine
 (driver, user):%0A
@@ -2227,24 +2227,32 @@
        login
+_routine
 (driver, use
@@ -2953,17 +2953,25 @@
  print %22
-G
+User %25s g
 ame over
@@ -2972,16 +2972,34 @@
 e over.%22
+ %25 user%5B%22account%22%5D
 %0A%0Adef i_
@@ -3260,14 +3260,372 @@
 _':%0A    
-main(
+parser = argparse.ArgumentParser(description='Compile smart contracts')%0A%0A    parser.add_argument('--game_type', %5C%0A                        help='Its value is one of %25s and %25s' %25 ('login', 'dice'),%0A                        default='login')%0A%0A    args = parser.parse_args()%0A%0A    game = %7B%0A        %22login%22: login,%0A        %22dice%22: dice_game%0A    %7D%0A%0A    main(args.game_type
 )

Add standalone login function and multiple account to do the same thing