问题遇到的现象和发生背景
在服务器中搭建python使用selenium,会报错,在我电脑环境可以正常运行,
问题相关代码,请勿粘贴截图
#coding=utf-8
import configparser
import json
import os
import time
import pyautogui
import pydirectinput
import requests
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
def Main():
cfp = configparser.ConfigParser()
cfp.read("test.ini")
if cfp.has_section("系统配置"):
chushihua_liulaiqi(cfp.get('系统配置', "url"))
open_liulanqi(cfp.get('系统配置', "url"))
login(cfp.get('系统配置', "user_name"), cfp.get('系统配置', "password"), cfp.get('系统配置', "url"))
up_video(cfp.get('系统配置', "up_video_path"), wenan(cfp.get('系统配置', "wenan")), cfp.get('系统配置', "url"))
print('视频已经发布成功')
else:
print("配置文件错误,请重新配置")
def chushihua_liulaiqi(url): # 初始化浏览器
os.popen('chrome.exe --remote-debugging-port=9527 --user-data-dir="D:/"') # popen与system可以执行指令,popen可以接受返回对象
options = Options()
options.add_experimental_option("debuggerAddress", "127.0.0.1:9527")
browser = webdriver.Chrome(options=options)
browser.get(url)\
def open_liulanqi(url): # 打开浏览器
options = Options()
options.add_experimental_option("debuggerAddress", "127.0.0.1:9527")
browser = webdriver.Chrome(options=options)
browser.maximize_window() # 最大化窗口
browser.get(url)
time.sleep(1)
return browser
def login(user, password, url): # 登录
browser = open_liulanqi(url)
browser.implicitly_wait(1)
browser.find_element(By.CLASS_NAME, "login").click()
browser.implicitly_wait(1)
browser.find_element(By.CLASS_NAME, "semi-button.semi-button-primary").click()
browser.find_element(By.XPATH, "//div[@class='semi-tabs-tab']").click()
browser.find_element(By.XPATH, "//div[@class='toggle']").click()
browser.find_element(By.XPATH,
"//div[@id='dialog-0']/div/div[2]/div/div[2]/div[2]/div/form/div/div/div/input").send_keys(
user)
browser.find_element(By.XPATH, "//input[@value='']").send_keys(password)
browser.find_element(By.XPATH, "//div[@id='dialog-0']/div/div[2]/div/div[2]/div[2]/div/form/div[4]/img").click()
browser.find_element(By.XPATH, "//div[@id='dialog-0']/div/div[2]/div/div[2]/div[2]/div/form/button/span").click()
def up_video(video_name, wenan, url): # 上传视频
browser = open_liulanqi(url)
browser.implicitly_wait(5)
browser.find_element(By.XPATH, "//div[@id='root']/div/div[2]/div[2]/div/div/div/div/button/span").click() # 上传视频按钮
browser.implicitly_wait(10)
browser.find_element(By.CLASS_NAME, "upload--2sMLw").click() # 选择视频按钮
browser.implicitly_wait(1)
pydirectinput.moveTo(124, 50) # 将鼠标移动到
pydirectinput.click()
time.sleep(1)
pyautogui.typewrite(video_name, interval=0.25) # 视频路径
time.sleep(1)
pyautogui.typewrite(['enter'] * 2, interval=0.1) # 确认视频路径
time.sleep(1)
pydirectinput.moveTo(229, 171) # 选择视频
pydirectinput.click()
pydirectinput.moveTo(780, 507) # 点击选择视频确定
pydirectinput.click()
browser.implicitly_wait(5)
browser.find_element(By.XPATH, "//div[@id='content']/div/div[2]/div/div/div/div").send_keys(wenan) # 文案框
browser.implicitly_wait(20)
browser.find_element(By.XPATH, "//div[@id='root']/div/div/div[2]/div/div[14]/button").click() # 上传按钮
browser.implicitly_wait(10)
# 删除视频
browser.find_element(By.XPATH, "//div[@id='root']/div/div[2]/div[2]/div/div/div/div/button/span").click() # 上传视频按钮
browser.implicitly_wait(10)
browser.find_element(By.CLASS_NAME, "upload--2sMLw").click() # 选择视频按钮
time.sleep(1)
pydirectinput.moveTo(124, 50) # 将鼠标移动到
pydirectinput.click()
time.sleep(1)
pyautogui.typewrite(video_name, interval=0.25) # 视频路径
time.sleep(1)
pyautogui.typewrite(['enter'] * 2, interval=0.1) # 确认视频路径
time.sleep(1)
pydirectinput.moveTo(229, 171) # 选择视频
pydirectinput.click()
pyautogui.hotkey('ctrl', 'd') # 删除视频
pyautogui.hotkey('alt', 'f4') # 关闭文件筐
#
browser.implicitly_wait(2)
browser.find_element(By.XPATH, "//div[@id='header-avatar']/div").click() # 点击头像
browser.implicitly_wait(2)
browser.find_element(By.CLASS_NAME, "logout").click() # 点击头像
browser.close()
def wenan(url): # 获取文案
r = requests.get(url)
s = r.text.encode('utf-8').decode('unicode_escape')
json1 = json.loads(s, strict=False)
wenan = json1.get('data').get('content')
return wenan
def video_name(): # 获取视频名称
cfp = configparser.ConfigParser()
cfp.read("test.ini")
if __name__ == '__main__':
Main()