七苦、 2021-10-20 14:02 采纳率: 0%
浏览 75
已结题

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"
    }
  }
}
  • 写回答

3条回答 默认 最新

  • CSDN专家-Time 2021-10-20 14:04
    关注

    用xpath定位。

    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 10月21日
  • 修改了问题 10月21日
  • 创建了问题 10月20日

悬赏问题

  • ¥20 Python安装cvxpy库出问题
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥15 python天天向上类似问题,但没有清零
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题