不知道为什么总是定位不到元素的位置,麻烦看一下
import time
from telnetlib import EC
import pygetwindow
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
import pyautogui
class test_main:
#定义一个秒
second = 60
def test_setup(self):
#打开浏览器
self.driver = webdriver.Chrome()
# 打开网站
self.driver.get("https://www.51zxw.net/login/")
# 窗口最大化
self.driver.maximize_window()
#隐试等待5秒 (方便找到元素)
self.driver.implicitly_wait(3)
# 2.登入操作
def test_login(self):
# 2.2 因为有默认值所有点击输入框先清空原来的账号密码
# self.driver.find_element(By.CSS_SELECTOR, "[placeholder='用户名/邮箱/手机号']").clear()
self.driver.find_element(By.CSS_SELECTOR, "[placeholder='用户名/邮箱/手机号']").send_keys("15860680887")
# self.driver.find_element(By.CSS_SELECTOR, "[placeholder='密码']").clear()
self.driver.find_element(By.CSS_SELECTOR, "[placeholder='密码']").send_keys("15860680887a")
self.driver.find_element(By.XPATH,"//*[@id='pcLoginMode']/div[4]/button").click()
time.sleep(1)
self.driver.find_element(By.XPATH,"/html/body/div[2]/div/a[1]").click()
#鼠标向上滚动 1个单位 = 120PX
# pyautogui.scroll(1)
# 2.3 使用横纵坐标的方式
ActionChains(self.driver).scroll_by_amount(0,350).perform()
time.sleep(1)
# 2.4点击电脑新手视频的标题
# self.driver.find_element(By.XPATH,"//img[@src='https://image.51zxw.net/m/img/kcbm/1053.jpg']")
self.driver.find_element(By.XPATH,"/html/body/div[3]/div[4]/a[1]/div[1]/img").click()
time.sleep(3)
def one_test(self):
# 切换到新页面并执行操作
handles = self.driver.window_handles
for handle in handles: # 切换窗口(切换到搜狗)
if handle != self.driver.current_window_handle:
print
'switch to second window', handle
self.driver.switch_to.window(handle)
# 2.5多窗口的使用跳转到目录页面
# windows = self.driver.window_handles
# 跳转到最后一个窗口 从0开始
# self.driver.switch_to.window(windows[1])
current_window = self.driver.current_window_handle
print("第一个窗口 "+self.driver.current_window_handle)
time.sleep(2)
#强制等待 5秒查找元素
element = WebDriverWait(self.driver, 5).until(
EC.presence_of_element_located((By.XPATH, "/html/body/div[4]/div[1]/div[1]/ul/li[3]/a")))
# 点击运行的三角号
print("第二个窗口 " + self.driver.current_window_handle)
print("打印所有窗口 ")
print(self.driver.window_handles)
# 第一章:认识电脑和操作系统 认识电脑设备和常见类型"
self.driver.find_element(By.XPATH,"/html/body/div[4]/div[1]/div[1]/ul/li[3]/a").click()
# 打印最后一个窗口
# self.driver.switch_to.window(windows[-1])
# 将鼠标移动到(x,y)坐标位置,然后左击一下
print("当前窗口 " + self.driver.current_window_handle)
time.sleep(5)
# 获取当前活动窗口的句柄
active_window = pygetwindow.getActiveWindow()
active_window_handle = active_window.handle
# 遍历窗口并比较句柄
for window in pygetwindow.getAllWindows():
if window.handle == active_window_handle:
# 当前窗口的句柄与活动窗口的句柄相同
print("找到当前窗口")
self.driver.find_element(By.XPATH, "//*[@id='chvqhsntkpeptdoygx']").click()
time.sleep(10 * 60)
else:
# 没有找到当前窗口
print("未找到当前窗口")
# # 将光标移动到目标位置
# pyautogui.moveTo(2720, 580)
# # 模拟按下空格键
# pyautogui.press('space')
# 点击播放的按钮
time.sleep(10*60)
print("点几三角号成功")
# 回退到上一个界面
# self.driver.back()
time.sleep(15)
def test_exit(self):
self.driver.quit()
if __name__ =='__main__':
a = test_main()
a.test_setup()
a.test_login()
a.one_test()
# 退出
a.test_exit()
有空给你看看
不知道你这个问题是否已经解决, 如果还没有解决的话:所有功能函数都是操作学员信息,所有存储所有学员信息应该是一个==全局变量==,数据类型为==列表==。
info = []
1.3.4.1 添加学员
需求分析
接收用户输入学员信息,并保存
判断是否添加学员信息
2.1 如果学员姓名已经存在,则报错提示
2.2 如果学员姓名不存在,则准备空字典,将用户输入的数据追加到字典,再列表追加字典数据
对应的if条件成立的位置调用该函数
代码实现
def add_info():
""" 添加学员 """
# 接收用户输入学员信息
new_id = input('请输入学号:')
new_name = input('请输入姓名:')
new_tel = input('请输入手机号:')
# 声明info是全局变量
global info
# 检测用户输入的姓名是否存在,存在则报错提示
for i in info:
if new_name == i['name']:
print('该用户已经存在!')
return
# 如果用户输入的姓名不存在,则添加该学员信息
info_dict = {}
# 将用户输入的数据追加到字典
info_dict['id'] = new_id
info_dict['name'] = new_name
info_dict['tel'] = new_tel
# 将这个学员的字典数据追加到列表
info.append(info_dict)
print(info)