- 安装必要环境
- linux环境配置
- 基本的使用
- 创建浏览器控制
- 访问网页
- 获取元素
- By
- 等待浏览器相应
- 元素交互
- 网页游戏的挂载
- 服务器程序不挂断
- window 下安装
- linux下配置
- 基本的使用
- 实现网页游戏的挂载
- 安装selenium
pip install selenium
- 安装浏览器
这里我使用自带的Edge浏览器 - 安装浏览器driver
根据自己浏览器的版本,去下载Edgedriver,只需要前面的大版本一致就可以,但是还是尽量满足所有的版本一致比较好
EdgeDriver所有版本 - 环境变量配置
可以将下载的degedriver.exe文件放到自己的python文件夹中,或者将degedriver.exe的父文件夹加入到系统的path中,这样就可以运行
我的服务器是ubuntu19
对于linux应该采用chrome浏览器,因为selenium中内置了ChromeOption,可选择关闭浏览器的界面,对于linux的命令式系统是十分契合的
- 下载chrome
linux版本的chrome - 下载chromedriver
各种版本的chromedriver - 安装chrome,解压chormedriver
将下载的浏览器,和driver通过ftp传到服务器上
执行
unzip chromedriver_linux64.zip
会看到deb和rmp压缩文件,Ubuntu使用deb,centos使用rmp
apt install ./google-chrome-stable_current_amd64.deb
执行
unzip chromedriver_linux64.zip
- 移动chromedriver,增加权限
需要将chrome移动到/usr/bin目录下
执行
mv chormdriver /usr/bin
增加权限
chmod +x /usr/bin/chromedriver基本的使用
window下
创建浏览器控制from selenium import webdriver from selenium.webdriver.common.by import By import time driver = webdriver.Edge()访问网页
driver.get('https://www.baidu.com')
获取元素
- driver.find_element(By,…)
- driver.find_elements(By,…)
最主要的就是这两个方法
前者:当有多个匹配时,返回第一个elementDriver,当未找到时,程序报错中断
后者:返回一个列表,列表内元素都是元素控制器,当未找到时返回一个空列表
对于selenium4提倡使用这种By的方式
例如
idFirst = driver.get_element(By.ID, 'first')等待浏览器相应
因为程序执行是很快的,但是浏览器渲染,解析是耗时的,这就需要程序去等待浏览器渲染出相应的元素才能get到
driver = webdriver.Edge() driver.implicitly_wait(10)
在创建浏览器控制器时,就可以指定一个等待时间,仅仅在get_element和get_elements时进行停止等待,当超过预定的时间时,才会报错或者返回空列表。当在预定的时间内找到元素时,程序继续运行
元素交互- send_key()
对于input输入框可以使用elementDriver.send_key(“text”)的方式,向其中输入text文字 - click()
对于button类型的组件可以使用elementDriver.click()来模拟点击操作 - clear()
对于input想要清除其中的输入,可以使用elementDriver.clear()来进行清空
基本上就会用到这三个吧
最近在玩一个挂机游戏,后台运行还是不错的。
匿名修仙
最近嫖了个服务器,准备在上面挂一挂这个网页游戏
-
连点 - 指定移动
- 降妖任务
- 采药任务
- 宗门任务
linux下运行
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver import ActionChains
from selenium.webdriver.chrome.webdriver import Options
import time
def main():
print("程序运行:", time.strftime("%Y/%m/%d %H:%M:%S"))
chrome_option = Options()
chrome_option.add_argument('--no-sandbox')
chrome_option.add_argument('--headless')
driver = webdriver.Chrome(options=chrome_option)
driver.implicitly_wait(10)
driver.get('https://game.nimingxx.com/login')
inputs = driver.find_elements(By.CLASS_NAME, 'el-input__inner')
inputs[0].send_keys("")
inputs[1].send_keys("n")
login = driver.find_elements(By.TAG_NAME, "button")
login[0].click()
# end------------------------
# 刷新页面---------------------
time.sleep(2)
tagList = driver.find_elements(By.CLASS_NAME, "ant-badge")
driver.execute_script("arguments[0].click();", tagList[8])
time.sleep(.5)
# 签到,领维护--------------------
signButton = driver.find_element(By.XPATH, '//*[@id="activity"]/div[1]/div[2]/button[1]')
signButton.click()
maintainButton = driver.find_element(By.XPATH, '//*[@id="activity"]/div[1]/div[2]/button[2]')
maintainButton.click()
print("签到成功", end='、')
# end---------------------------
for i in range(3):
driver.execute_script("arguments[0].click();", tagList[3])
driver.execute_script("arguments[0].click();", tagList[2])
print("刷新页面成功")
time.sleep(.5)
# end----------------------------
# 密令开关
messButton = driver.find_element(By.XPATH, '//*[@id="pane-scene-tab"]/div[2]/div[1]/div[1]/label/span[1]/span')
driver.execute_script("arguments[0].click()", messButton)
# end------------------
# 创建队伍
createTeam = driver.find_element(By.XPATH, '//*[@id="pane-scene-tab"]/div[2]/div[1]/div[1]/button[1]')
createTeam.click()
print("创建密码队伍成功", end='、')
# end -----------------
# 自己创建的队伍
time.sleep(.5)
myTeam = driver.find_element(By.XPATH, '//*[@id="pane-scene-tab"]/div[2]/div[1]/div[2]/div/div[1]/div/div[2]/div['
'1]')
if myTeam.get_attribute('outerText').split()[0] == '':
time.sleep(.5)
clickPath = driver.find_element(By.CSS_SELECTOR, '#pane-scene-tab > div:nth-child(3) > div.el-col.el-col-10 > '
'div:nth-child(2) > div > div.__panel > div > div:nth-child('
'3) > '
'div.el-col.el-col-10 > i > svg')
ActionChains(driver).move_to_element(clickPath).perform()
print("鼠标移动成功", end='、')
time.sleep(.5)
else:
print("未找到密码")
try:
time.sleep(.5)
messText = driver.find_element(By.XPATH, '/html/body/div[3]/div/div/div/div[2]').get_attribute('innerText')
print("当前队伍:", messText, end='、')
except Exception as e:
print(e)
time.sleep(2)
# 可选战斗列表
List = driver.find_elements(By.XPATH, '//*[@id="pane-scene-tab"]/div[2]/div[2]/div[2]/div')
monsterDict = {}
for i in List[0:-1]:
# print(i.find_element(By.XPATH, './div[1]/span/span').get_attribute('innerText'))
monsterDict[
i.find_element(By.XPATH, './div[1]/span/span').get_attribute('innerText').split()[0]] = i.find_element(
By.XPATH,
'./div[2]/a/div/img')
monsterDict['落樱灵兽群'].click()
# end-----------------------
# 技能选择--------------------
lyxx = driver.find_element(By.XPATH, '//*[@id="s-info16"]/img')
lyxx.click()
print("技能选择成功", end='、')
# end-----------------------
# 自动放技能------------------
auto = driver.find_element(By.XPATH, '//*[@id="pane-bat-tab"]/div[2]/div[2]/div[2]/div[2]/label/span[2]')
auto.click()
print("技能开启成功")
# end-----------------------
# 循环挑战
# circulate = driver.find_element(By.XPATH, '//*[@id="pane-bat-tab"]/div[2]/div[1]/div[2]/label/span[2]')
# circulate.click()
# time.sleep(10)
# end----------------------
# 关闭显示-----------------
offlog = driver.find_element(By.XPATH, '//*[@id="Index"]/div[2]/div[3]/div/div/div[2]/div[3]/div[1]/div[2]/div/span')
driver.execute_script("arguments[0].click();", offlog)
# offlog.click()
print("日志关闭成功", end='、')
# end---------------------
# 结算日志-----------------
log = driver.find_element(By.XPATH,
'//*[@id="Index"]/div[2]/div[3]/div/div/div[2]/div[1]/div/div/div/div/div[1]/div[2]')
driver.execute_script("arguments[0].click();", log)
# log.click()
print("结算日志切换成功")
# end--------------------
# 信息-------------------
infos = driver.find_element(By.XPATH,
'//*[@id="Index"]/div[2]/div[3]/div/div/div[2]/div[3]/div[2]/div[2]/div[2]/div[1]')
# end--------------------
# 连点--------------------
again = driver.find_element(By.XPATH, '//*[@id="pane-bat-tab"]/div[2]/div[1]/div[2]/a')
print("连点按钮获取成功,当前攻击的野怪是{}".format('落樱灵兽群'))
print("开始连点:", time.strftime("%Y/%m/%d %H:%M:%S"))
count = 1
before = 0
num = 0
while True:
time.sleep(2.1)
again.click()
if count % 50 == 0:
print("点击了{}次: ".format(count), time.strftime("%Y/%m/%d %H:%M:%S"),end='、')
# infoList = infos.find_elements(By.CSS_SELECTOR, 'p') # 累计胜利, 累计败北,累计经验
# print(infoList[0].get_attribute('innerText').split()[0])
experience = driver.find_element(By.XPATH, '//*[@id="step_a"]/div[1]').get_attribute('innerText')
# 出现不加经验的情况
if experience == before:
num += 1
if num == 2:
raise
before = experience
print("总体经验:", experience)
count += 1
# end-------------------
if __name__ == '__main__':
try:
main()
except Exception as e:
print("程序异常终止!", e)
服务器程序不挂断
- 对于服务器上的程序在服务器连接断开时,希望其在后台继续运行,就要将其固定到一个端口上
执行nohup python3 -u ./nimingxx_0.1.py > log.out 2>&1 &
log.out是输出日志,子啊程序中执行一次回车操作就会将控制台的输出写到文件中 - 可以使用supervisor
-
安装supervisor
apt-get install supervisor
-
配置要进行的command
vim /etc/supervisor/supervisord.conf
在文件的后面加上
[program:Junziyuechuan] command=python3 /home/nimingxx/nimingxx_0.1.py autostart=true autorestart=true stdout_logfile=/home/nimingxx/out.log stderr_logfile=/home/nimingxx/err.log
program:自己程序取的名称
command: 就是要开始执行的命令
autostrat:服务器启动是否自动启动
autostrat:服务器重启是否自动重启
stdout_logfile:正常日志输出 -
启动服务
执行supervisord
这错误,是在第一次运行时要指定conf文件,就是上面编辑的那个文件supervisord -c /etc/supervisor/supervisord.conf
这个错误就是已经开启了一个端口用来跑supervisor,需要将其kill
查询端口号ps -aux|grep supervisord
kill 端口即可
kill -9 端口号
出现这个
出现这个错误,就是自己的supervisord.conf配置出错了
我就出现了错误
原因是,我的python环境是配置的虚拟环境,而上面的command我配置的是全局的python解释器,这样就会报找不到包的错误,所以我就要更改commandcommand=/home/nimingxx/selenium3.8/bin/python3 /home/nimingxx/nimingxx_0.1.py
然后shutdown,重启就正常了
这里有一些常用的supervisor指令service supervisor stop #停止supervisor服务 service supervisor start #启动supervisor服务 supervisorctl shutdown #关闭所有任务 supervisorctl stop|start program_name #启动或停止服务 supervisorctl status #查看所有任务状态
很大可能会报错
-
create 2022/5/1
add 2022/5/3



