【问题标题】:Using python file as the data source for another one使用 python 文件作为另一个文件的数据源
【发布时间】:2018-09-24 16:43:48
【问题描述】:

我的以下代码运行良好, 我想让 python 文件从另一个文件中读取 userpwd, 所以如果我想登录任何其他帐户, 我将在不是程序源文件的文件中更改它, 我该怎么做?

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
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.webdriver.support.select import Select
import time
from time import sleep
import os
import sys



user = '.......'
pwd = '........'

driver = webdriver.Chrome()

driver.get('URL')
driver.maximize_window()
main_window = driver.current_window_handle

#wait username
wait_username = WebDriverWait(driver, 60)
wait_username.until(EC.element_to_be_clickable((By.ID,'..........')))

User_Name = driver.find_element_by_id("..........")
User_Name.send_keys(user)

#wait password
wait_password = WebDriverWait(driver, 60)
wait_password.until(EC.element_to_be_clickable((By.ID,'..........')))

Pass_Word = driver.find_element_by_id("..........")
Pass_Word.send_keys(pwd)

【问题讨论】:

    标签: python-3.x selenium-webdriver configuration config configuration-files


    【解决方案1】:

    我想说实现你想要的最简单的方法是使用configparser 库。您必须创建一个包含所需数据的 ini 文件。我们称这个文件为 config.ini

    [TestEnv]
    user = Zasados
    password = qwerty
    

    然后你必须在你的 Python 代码中读取这个文件的内容:

    import configparser
    
    config = configparser.ConfigParser()
    config.read('config.ini')
    default_config = config['TestEnv']
    print(default_config['user']) # prints out Zasados
    print(default_config['password']) # prints out qwerty
    

    使用这种方法可以让您使用不同的凭据保留不同的部分,然后对脚本进行参数化,这样您就不必编辑任何文件。请记住,出于安全原因,您不应将配置文件保留在存储库中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-21
      • 1970-01-01
      • 2013-02-03
      • 2016-07-01
      相关资源
      最近更新 更多