python+selenium无法定位到元素

img


常规定位元素方法基本上都试过了,定位不到。各位看看还有什么方法能进行元素定位

from tkinter import Image
import ddt
import unittest2
from upl.webUtil import *
#
import pytesseract
from PIL import Image
from PIL import Image, ImageEnhance
import os
import time
@ddt.ddt()
class UPL_Test(unittest2.TestCase):
    def setUp(self) -> None:
        self.driver = WebDriverUtil(path='chromedriver.exe')
        self.driver.get("http://192.168.59.122/user/login")
        self.driver.read("xpath.json")

    @ddt.file_data("data.json")
    def test(self, login):
        # 登录
        self.login(login)
        #音视频管理
        self.video()
    def login(self, login):
        self.driver.section = "login"
        # 进入登录页面
        # self.driver.get(login["url"])
        # 用户名输入
        self.driver.find_element("name").send_keys(login["name"])
        # 密码输入
        self.driver.find_element("password").send_keys(login["password"])
        # 获取验证码
        self.driver.find_element("code").send_keys(login["code"])#yzm()
        # 点击登录按钮
        self.driver.driver.implicitly_wait(5)
        self.driver.find_element("button").click()
    def video(self):
        self.driver.section="video"
        time.sleep(2)
        #点击节目单管理
        self.driver.find_element("programme").click()
        #新增
        self.driver.find_element("p_add").click()
        time.sleep(10)

if __name__ == '__main__':
    unittest2.main()

xpath.json

{
 "login": {
    "name": {
      "type": "id",
      "value": "username"
    },
   "password": {
     "type":"id",
     "value": "password"
   },
   "code": {
     "type": "id",
     "value": "inputCode"
   },
   "button": {
     "type": "xpath",
     "value": "//*[@id='formLogin']/div[2]/div/div/span/button"
   },
   "code_image": {
     "type": "xpath",
     "value": "//form[@id='formLogin']/div[1]/div[3]/div/div[4]/div[2]/img"
   }
 },
  "video": {
    "programme": {
     "type": "xpath",
     "value": "//*[@id='app']/section/aside/div/ul/li[9]/ul/li[2]/a/span"
   },
    "p_add": {
      "type": "xpath",
      "value": "//*[@id='app']/section/section/main/div[2]/div/div/div/div[2]/button"
    }
  }
}

用xpath定位。

要等待加载完成后再去定位,可用webdriverwait...until试试。

有些可能是Frame里面的元素,可能需要先切进去frame,再去找它里面的元素。