简体   繁体   English

接受用户输入并在另一个脚本中使用它

[英]Taking user input and using it in another script

I'm developing a python script to enter some corporative websites and extract the data that I need.我正在开发一个 python 脚本来进入一些公司网站并提取我需要的数据。 So I have two kinds of scripts, the first one is to do the automation process and the second type is the "parent" script in which I use runpy to run the first kind of scripts and tkinter.simpledialog.askstring to ask the user their login and password.所以我有两种脚本,第一种是执行自动化过程,第二种是“父”脚本,我使用runpy运行第一种脚本和tkinter.simpledialog.askstring询问用户他们的登录名和密码。 The problem that I'm facing is that I need to store the user's input (that I gather in my "parent" script) and use it in my automation script.我面临的问题是我需要存储用户的输入(我收集在我的“父”脚本中)并在我的自动化脚本中使用它。 This is my "parent" script:这是我的“父”脚本:

import runpy
import pyautogui
import pyperclip
import tkinter as tk
import tkinter.simpledialog

pyautogui.PAUSE=1

tk.Tk().withdraw()
login=tkinter.simpledialog.askstring("Login","Enter login", show="")
password=tkinter.simpledialog.askstring("Password","Enter password", show="*")
time.sleep(2)
runpy.run_path('path to my automation script')

As you can see, it just stores the user's input and run another script.如您所见,它只是存储用户的输入并运行另一个脚本。 Below is the another type of script that is located on 'path to my automation script'.下面是另一种类型的脚本,它位于“我的自动化脚本的路径”上。

from selenium import webdriver
import pyforest
import time
import pandas
import openpyxl
import pyautogui
from Parent_Script import login, password

driver=webdriver.Chrome(executable_path='path to chromedriver',options=options)
driver.get('corporative website')

login_website=driver.find_element_by_xpath('//*[@id="txtLogin"]')
login_website.send_keys(login)
password_website=driver.find_element_by_xpath('//*[@id="txtPassword"]')
password_website.send_keys(password)

I'm trying to import my parent script variables login and password but as a user I have to fill the tkinter dialogs two times (I think that the first fill occurs because I run the Parent script and the second one it's when the Parent script runs the Automation script and thus it re-run the Parent script to do the variable import).我正在尝试导入我的父脚本变量登录名和密码,但作为用户,我必须填写 tkinter 对话框两次(我认为第一次填写是因为我运行父脚本而第二次填写是在父脚本运行时自动化脚本,因此它重新运行父脚本来执行变量导入)。 Is there a better (or right) way to make it work effectively?有没有更好(或正确)的方法可以让它有效地工作?

While I was waiting for an answer I've found a solution.在等待答案时,我找到了解决方案。 Well, kind of .嗯,有点

I removed all tkinter stuff from my "Parent" script and created a module named "Login_Password".我从“父”脚本中删除了所有tkinter内容,并创建了一个名为“Login_Password”的模块。 The whole script of this module is:这个模块的整个脚本是:

import tkinter as tk
import tkinter.simpledialog

tk.Tk().withdraw()
login=tkinter.simpledialog.askstring("Login","Enter login", show="")
password=tkinter.simpledialog.askstring("Password","Enter password", show="*")

Then I moved my Login_Password module to the path of site-packages from Python to be able to import it as a module in my automation script and it's working fine now.然后我将我的 Login_Password 模块从 Python 移动到站点包的路径,以便能够将其作为模块导入到我的自动化脚本中,现在它工作正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM