126メールボックス新規連絡先自動化スクリプト


from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException, NoSuchElementException
import time, traceback

driver = None
wait = None

#    
def login_126():

    global driver
    global wait
    driver = webdriver.Chrome(executable_path="d:\\chromedriver")
    url = "http://www.126.com"
    driver.get(url)
    #         
    wait = WebDriverWait(driver, 10, 0.2)
    try:
        #         iframe
        wait.until(EC.frame_to_be_available_and_switch_to_it(( \
            By.XPATH, '//iframe[contains(@id,"x-URS-iframe")]')))
        time.sleep(3)
        # driver.switch_to.frame(driver.find_element_by_xpath(\
        # '//iframe[contains(@id,"x-URS-iframe")]'))

        #                       
        username = wait.until(lambda x: x.find_element_by_xpath( \
            '//input[@placeholder="        "]'))

        username.send_keys("xxx")

        #          
        passwd = driver.find_element_by_xpath('//input[@name="password"]')
        passwd.send_keys("xxxxx")

        #          
        driver.find_element(By.ID, "dologin").click()

        #     
        driver.switch_to.default_content()

        #          “  ”        
        if wait.until(EC.visibility_of_element_located(('xpath', '//a[.="  "]'))):
            print("    !")
        else:
            print("    !")

    except NoSuchElementException as e:
        print(traceback.print_exc())
    except TimeoutException as e:
        print(traceback.print_exc())
    except Exception as e:
        print(traceback.print_exc())

#              
def close_driver():
    global driver
    driver.quit()

#     
def add_contact():
    global driver
    global wait
    try:
        #    “   ”    ,     
        address_book = wait.until(EC.element_to_be_clickable(("xpath",'//div[.="   "]')))

        address_book.click()

        #             ,       
        new_contact = wait.until(lambda x : x.find_element_by_xpath('//span[.="     "]'))
        #wait.until(EC.element_to_clickable('xpath','//span[.="     "]'))

        new_contact.click()

        #            “  ”     ,       
        nameBox = wait.until(EC.visibility_of_element_located(('id','input_N')))
        nameBox.send_keys("hhhhhh")

        #      、          
        emailBox = driver.find_element_by_xpath('//div[@id="iaddress_MAIL_wrap"]//input')
        emailBox.send_keys("[email protected]")

        #            
        starBox = driver.find_element_by_xpath(\
             '//span[.="       " and @class="nui-chk-text"]//preceding-sibling::*/b')
        starBox.click()

        phoneBox = driver.find_element_by_xpath(\
              '//div[@id="iaddress_TEL_wrap"]//input')

        phoneBox.send_keys("13122222222")

        commentBox = driver.find_element_by_id("input_DETAIL")
        commentBox.send_keys("hhhh")

        #      
        driver.find_element_by_xpath('//span[.="   "]').click()

        #                 
        #emailStr = "[email protected]"
        if wait.until(EC.visibility_of_element_located(('xpath','//nobr[.="[email protected]"]'))):
            print("       !")
        else:
            print("       !")

    except NoSuchElementException as e:
        print(traceback.print_exc())
    except TimeoutException as e:
        print(traceback.print_exc())
    except Exception as e:
        print(traceback.print_exc())

def main():
    global driver
    global wait

    login_126()  
    add_contact()
    close_driver()

if __name__ == "__main__":
    main()