努力的编程的小白1 2023-07-14 22:46 采纳率: 50%
浏览 26

python自动化一直找找不到元素定位的位置,已经卡了好多天了

img

不知道为什么总是定位不到元素的位置,麻烦看一下


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

  • 写回答

2条回答 默认 最新

  • winx96 2023-07-15 00:35
    关注

    有空给你看看

    评论

报告相同问题?

问题事件

  • 创建了问题 7月14日