1 virtualenv简介
是专门用来为应用程序维护多套相互“隔离”的Python运行环境的工具,借助 virtualenv我们可以在一台主机上创建多套相互隔离的Python运行环境。
所以呢需要下载好我已经配置好的虚拟环境才能正常运行下面的程序。
2 .virtualenv的配置
python环境是python3.7,需要用到的库文件pyqt5,pyqt5-tools,playwright,os
浏览器的驱动文件
如果已经安装了python环境,则直接执行 python -m playwright install,否则需要先进入虚拟环境再执行上面的命令。
1录制自动化脚本,只要输入对应的网址和浏览器(现在只支持谷歌),点击star按键即可,后续更新会解决这个问题(调试的时候火狐,webkit等有一定的bug)。然后就可以按照自己的想法录制步骤,右边是自动生成的python代码。录制完成后就可以关闭浏览器。最后就会生成一个code.txt文件。
2运行脚本。首先把生成的code文件的后缀改为py,然后在脚本输入框输入**py文件的绝对路径 **,点击start__script。就会执行自动化脚本
"""
brower.py文件
"""
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'brower_automation.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
import os
class Ui_MainWindow(object):
def __init__(self):
self.url = "www.baidu.com"
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(563, 326)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(270, 10, 301, 101))
self.label.setObjectName("label")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(120, 70, 101, 21))
self.pushButton.setObjectName("pushButton")
self.pushButton.clicked.connect(self.start_PushBotton)
self.radioButton = QtWidgets.QRadioButton(self.centralwidget)
self.radioButton.setGeometry(QtCore.QRect(30, 90, 89, 16))
self.radioButton.setObjectName("radioButton")
self.radioButton_2 = QtWidgets.QRadioButton(self.centralwidget)
self.radioButton_2.setGeometry(QtCore.QRect(30, 70, 89, 16))
self.radioButton_2.setObjectName("radioButton_2")
self.radioButton_3 = QtWidgets.QRadioButton(self.centralwidget)
self.radioButton_3.setGeometry(QtCore.QRect(30, 50, 89, 16))
self.radioButton_3.setObjectName("radioButton_3")
self.radioButton_3.setChecked(True)
self.ShowText = QtWidgets.QTextBrowser(self.centralwidget)
self.ShowText.setGeometry(QtCore.QRect(20, 210, 521, 111))
self.ShowText.setObjectName("ShowTesx")
self.ShowText.setPlaceholderText("""Prompt information:
open page and generate code for user actions
Options:
-o, --output saves the generated script to a file
--target language to use, one of javascript, python, python-async, csharp (default: "python")
-h, --help display help for command
Status operation information:
""" )
self.Show_Label = QtWidgets.QLabel(self.centralwidget)
self.Show_Label.setGeometry(QtCore.QRect(240, 180, 101, 31))
self.Show_Label.setObjectName("Show_Label")
self.scrupt_a = QtWidgets.QLabel(self.centralwidget)
self.scrupt_a.setGeometry(QtCore.QRect(30, 150, 101, 31))
self.scrupt_a.setObjectName("scrupt_a")
self.script_lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.script_lineEdit.setGeometry(QtCore.QRect(100, 155, 200, 20))
self.script_lineEdit.setObjectName("script_lineEdit")
self.scriptButton = QtWidgets.QPushButton(self.centralwidget)
self.scriptButton.setGeometry(QtCore.QRect(350, 155, 101, 21))
self.scriptButton.setObjectName("scriptButton")
self.scriptButton.clicked.connect(self.start_script)
self.url = QtWidgets.QLabel(self.centralwidget)
self.url.setGeometry(QtCore.QRect(30, 20, 51, 21))
self.url.setObjectName("url")
self.url_lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.url_lineEdit.setGeometry(QtCore.QRect(90, 20, 141, 21))
self.url_lineEdit.setObjectName("url_lineEdit")
self.url_lineEdit.setPlaceholderText("https://www.baidu.com/") # 默认显示
self.brower = QtWidgets.QLabel(self.centralwidget)
self.brower.setGeometry(QtCore.QRect(30, 120, 54, 16))
self.brower.setObjectName("brower")
self.brower_lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.brower_lineEdit.setGeometry(QtCore.QRect(90, 120, 141, 21))
self.brower_lineEdit.setObjectName("brower_lineEdit")
self.brower_lineEdit.setPlaceholderText("默认开启的是谷歌浏览器")
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.actionpyton = QtWidgets.QAction(MainWindow)
self.actionpyton.setObjectName("actionpyton")
self.actionyes = QtWidgets.QAction(MainWindow)
self.actionyes.setObjectName("actionyes")
self.actionno = QtWidgets.QAction(MainWindow)
self.actionno.setObjectName("actionno")
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
self.get_url()
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "浏览器自动化"))
self.label.setText(_translate("MainWindow",
"注意事项:
1.必须有python3.7+的环境
2.安装Chromiun、Firefox、Webkit等浏览器的环境
"))
self.pushButton.setText(_translate("MainWindow", "start"))
self.scriptButton.setText(_translate("MainWindow", "start_script"))
self.radioButton.setText(_translate("MainWindow", "webkit"))
self.radioButton_2.setText(_translate("MainWindow", "firefox"))
self.radioButton_3.setText(_translate("MainWindow", "chrome"))
self.Show_Label.setText(_translate("MainWindow",
"信息提示框
"))
self.scrupt_a.setText(_translate("MainWindow",
"脚本路径
"))
self.url.setText(_translate("MainWindow",
"URL
"))
self.brower.setText(_translate("MainWindow",
"浏览器
"))
self.actionpyton.setText(_translate("MainWindow", "pyton"))
self.actionyes.setText(_translate("MainWindow", "yes"))
self.actionno.setText(_translate("MainWindow", "no"))
def radio_status(self):
"""包含了所有的浏览器,默认是谷歌浏览器
问题:
1.浏览器的驱动是否安装;
2.浏览器驱动的名字没有确认
"""
if self.radioButton_3.isChecked() is True:
return "chromium"
elif self.radioButton_2.isChecked() is True:
return "Firefox"
elif self.radioButton.isChecked() is True:
return "webkit"
else:
return self.brower_lineEdit.text()
def get_url(self):
if len(self.url_lineEdit.text()) > 0:
url = self.url_lineEdit.text()
return url
url = "https://www.baidu.com/"
return url
def start_PushBotton(self):
url = self.radio_status()
brower = self.get_url()
os.chdir("D:")
os.system(
r"D:\python\virtualenv\demo\Scripts\activate | python -m playwright codegen --target python -o 'code.py' -b {} {}".format(
url, brower))
def start_script(self):
python_script = self.script_lineEdit.text()
os.system("D:")
os.system(r"D:\python\virtualenv\demo\Scripts\activate | python {}".format(python_script))
"""
main.py文件
"""
import sys
import brower
from PyQt5.QtWidgets import QApplication, QMainWindow
if __name__ == '__main__':
app = QApplication(sys.argv)
MainWindow = QMainWindow()
ui = brower.Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
根据上面装好相应的环境直接运行就可以了pyqt5, playwright, pyqt5-tools,还有各种浏览器的驱动。强烈建议使用第二种方法,直接粘贴代码,百分之九十都可以成功运行。
如有疑问或需数据请联系作者可留言博客或+
qq:1769190045
文章内容只供参考学习阅读,不可他用(特别商用)。侵权必追究其法律责任
——————————部分内容参考他人博客文章—————————————