zhyuping2002 发表于 2018-9-20 14:16:05

python + selenium 程序与数据分离

#python + selenium 程序与数据分离 开端
#userinfo.py 脚本

#!/usr/bin/python3
#coding=utf-8

def fun(un=username,pw=password):

    print("success reader username and password!")
    return(un,pw)



#login_python_userinfo_biaozhunhua.py脚本

#!/usr/bin/python3
#coding=utf-8

from selenium import webdriver
import unittest,time
import os
from selenium.webdriver.common.keys import Keys
import userinfo #倒入userinfo.py函数

class login_python_userinfo(unittest.TestCase):
    @classmethod
    def setUpClass(self):

      self.driver = webdriver.Firefox()
      driver = self.driver
      driver.get("http://www.baidu.com")
      time.sleep(2)
      print("打开百度网址成功")
      driver.maximize_window()
      driver.implicitly_wait(30)

    def test_login(self):
      self.us,self.pw = userinfo.fun()
      print(self.us,self.pw)
      driver = self.driver

      driver.find_element_by_xpath("/html/body/div/div/div/div/a").click()

      #try:
            #element0 = driver.find_element_by_name("tj_login")
            #for ele0 in element0:
                #if ele0.is_displayed():
                  #ele0.click()

      #except OSError as err:
         # print("OS Error: {0}".format(err))

      #except:
         # print("click_error or find_name_error")


      time.sleep(2)
      print("点击登录按钮成功")
      time.sleep(1)

      driver.find_element_by_id("TANGRAM__PSP_10__footerULoginBtn").click()
      print("点击 用户名登录 按钮")
      time.sleep(1)

      driver.find_element_by_id("TANGRAM__PSP_10__userName").send_keys(self.us)
      driver.find_element_by_id("TANGRAM__PSP_10__password").send_keys(self.pw)
      time.sleep(2)
      driver.find_element_by_id("TANGRAM__PSP_10__submit").click()
      print("登录成功")
      time.sleep(5)

    @classmethod
    def tearDownClass(self):
      self.driver.quit()

if __name__=='__main__':
    unittest.main()



qqq911 发表于 2019-3-13 10:34:27

感谢分享

jingzizx 发表于 2019-3-13 12:11:05

:lol
页: [1]
查看完整版本: python + selenium 程序与数据分离