【问题标题】:Taking user input and using it in another script接受用户输入并在另一个脚本中使用它
【发布时间】:2022-01-17 00:21:25
【问题描述】:

我正在开发一个 python 脚本来进入一些公司网站并提取我需要的数据。所以我有两种脚本,第一种是做自动化过程,第二种是“父”脚本,我使用 runpy 运行第一种脚本和 tkinter.simpledialog.askstring 询问用户他们的登录名和密码。 我面临的问题是我需要存储用户的输入(我收集在我的“父”脚本中)并在我的自动化脚本中使用它。这是我的“父”脚本:

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')

如您所见,它只是存储用户的输入并运行另一个脚本。下面是另一种类型的脚本,它位于“我的自动化脚本路径”上。

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)

我正在尝试导入我的父脚本变量登录名和密码,但作为用户,我必须填写 tkinter 对话框两次(我认为第一次填写是因为我运行 父脚本第二个是父脚本运行自动化脚本,因此它重新运行父脚本来进行变量导入)。有没有更好(或正确)的方法让它有效地工作?

【问题讨论】:

    标签: python selenium tkinter runpy


    【解决方案1】:

    在等待答案时,我找到了解决方案。嗯,有点

    我从“父”脚本中删除了所有 tkinter 内容,并创建了一个名为“Login_Password”的模块。这个模块的整个脚本是:

    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="*")
    

    然后我将我的 Login_Password 模块从 Python 移动到站点包的路径,以便能够将其作为模块导入到我的自动化脚本中,现在它工作正常。

    【讨论】:

      猜你喜欢
      • 2021-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-29
      • 2013-10-10
      • 2023-03-15
      • 2022-10-22
      • 2015-04-11
      相关资源
      最近更新 更多