编码版获取元素不稳定,有时候成功有时候失败
回答
收藏

编码版获取元素不稳定,有时候成功有时候失败
20

y
ytp
2025-10-11 11:31·浏览量:182
y
ytp
发布于 2025-10-11 11:24更新于 2025-10-11 11:31182浏览

做了一个淘宝上架脚本,在某几个地方获取元素有比较大的概率失败,事后单独运行那一行又是正常的(page = web.get_active('chrome', load_timeout=20)       page.find("销售规格_全屏").click(simulative=False)   ) ,比如这两处

代码

page.find("销售规格产品信息-新增确认").click()

page.find("销售规格_全屏").click(simulative=False)

报错

第【89】条指令出错:未找到元素, 元素名: 销售规格_全屏

【endScript】中第【244】条指令出错:匹配到多个元素, 无法唯一定位 元素名: 销售规格包含产品-确定

全部代码

import xbot

from xbot import print, sleep

from .import package

from .package import variables as glv

from xbot import print, sleep

import os

import json

import re

import xbot_visual

from .import process26

from xbot import web

# page = web.get_active('chrome', load_timeout=20)

page = glv['发布页面']

def main(args):

   encodeType = args['商家编码']

   # print(encodeType)

   # result = callByName("4.1获取类目属性",encodeType,outputs={})

   args1 = {"商家编码":encodeType,"产品属性字典":{}}

   process26.main(args1)

   print(args1["产品属性字典"])

   productProps = args1["产品属性字典"]

   # 获取要填写的属性

   if "品名" in productProps:

       productName = productProps.get("品名", "无品名")

   else:

       productName = productProps.get("糕点种类", "无品名")

   

   if "净含量" in productProps:

       netContent = productProps.get("净含量", "10g")

   else:

       netContent = productProps.get("单件净含量", "10g")

   # 1g/100g 这种情况没有处理 只处理基础情况 1g 1kg

   try:

       netContentNum = split_number_unit_decimal(netContent)[0]

       netContentUnit = split_number_unit_decimal(netContent)[1]

   except Exception as e:

       netContentNum = 10

       netContentUnit = 'g'

       pass

   size = productProps.get("包装规格", "1包")

   # 500g(含)-1000g(含)

   try:

       sizeNum = split_number_unit_decimal(size)[0]

       sizeUnit = split_number_unit_decimal(size)[1]

   except Exception as e:

       sizeNum = 1

       sizeUnit = '包'

       pass

   print("品名",productName)

   print("净含量",netContentNum,netContentUnit)

   print("包装规格",sizeNum,sizeUnit)


   # SKU分类必填的情况 有时候有有时候没有

   # 销售信息-SKU分类

   try:

      page.find("销售信息-SKU分类",timeout=5)

      eleList = page.find_all("销售信息-SKU分类-元素列表")

      for index, item in enumerate(eleList):

       # 第一个有值说明都有值不用处理

       if item.get_text():

           continue

       else:

           item.click(simulative=False)

           page.find("销售信息-SKU分类-元素列表下拉第一项").click(simulative=False)

   except Exception as e:

       print("销售信息-SKU分类错误",e)

   # 主推

   try:

      page.find("销售规格主推",timeout=5).click(simulative=False)

      page.find("销售规格推荐卖点店长主推").click(simulative=False)

   except Exception as e:

       print("销售规格主推不存在",e)

   def fun2():

       page.find("销售规格_全屏").click(simulative=False)

   retryFn(fun2)

   

   # 外部表单净含量列表

   try:

      isNetContent = page.find("销售规格外部净含量栏",timeout=3)

      eleList = page.find_all("销售规格外部净含量列表")

      for index, item in enumerate(eleList):

          item.click(simulative=False)

          try:

           page.find("销售规格外部净含量输入框",timeout=2).input(netContentNum)

           page.find("销售规格外部净含量下拉框").click()

           for item2 in page.find_all("销售规格外部净含量下拉框选项"):

               if item2.get_text() == netContentUnit:

                   item2.click()

                   break

           page.find("销售规格label").click()

          except Exception as e:

           page.find("销售规格label").click()

           print("没有找到销售规格-外部净含量输入框",e)

           continue          

   except Exception as e:

       print("销售规格净含量栏不存在",e)

   saleIncludeProd(netContentUnit,netContentNum,sizeUnit,sizeNum,productName)

   page.find("销售信息_退出全屏模式").click(simulative=False)

   

   try:

      page.find("条形码输入框_2",timeout=5).input("0000000000000")

   except Exception as e:

       print("条形码输入框2不存在",e)

   try:

      page.find("条形码输入框_3",timeout=5).input("0000000000000")

   except Exception as e:

       print("条形码输入框3不存在",e)

   # 基础信息   商品属性  净含量

   try:

       page.find("商品属性-净含量").input(netContentNum)

       page.find("商品属性-下拉框").click()

       for item in page.find_all("商品属性-下拉框内容"):

           if item.get_text() == netContentUnit:

               item.click()

   except Exception as e:

       print("基础信息   商品属性  净含量不存在",e)    

   

   # 基础信息-包装方式有多个

   try:

     page.find("基础信息-包装方式1",timeout=2).click()

     page.find("基础信息-包装方式-第一个选项").click()

   except Exception as e:

       print("基础信息-包装方式多个不存在",e)

       

   # 成分

   component()


def component():

   # 成分表

   try:

       page.find("成分表",timeout=2)

       page.find("成分-能量").input(5)

       page.find("成分-蛋白质").input(5)

       page.find("成分-脂肪").input(5)

       page.find("成分-碳水化合物").input(5)

       page.find("成分-钠").input(5)

   except Exception as e:

       print("成分不必填")

def component2():

   # 成分表

   try:

       page.find("产品信息-成分表",timeout=2)

       page.find("产品信息-成分-能量").input(5)

       page.find("产品信息-成分-蛋白质").click()

       page.find("产品信息-成分-蛋白质").input(5)

       

       page.find("产品信息-成分-脂肪").click()

       page.find("产品信息-成分-脂肪").input(5)

       page.find("产品信息-成分-碳水化合物").click()

       page.find("产品信息-成分-碳水化合物").input(5)

       page.find("产品信息-成分-钠").click()

       page.find("产品信息-成分-钠").input(5)

   except Exception as e:

       print("产品信息-成分不必填")

# 销售规格包含产品

def saleIncludeProd(netContentUnit,netContentNum,sizeUnit,sizeNum,productName):

       # 销售规格包含产品列表

   eleList = page.find_all("销售规格元素列表")

   for index, item in enumerate(eleList):

       item.click(simulative=False)

       # 有禁用的情况

       try:

           page.find("销售规格产品添加",timeout=1).click()

       except Exception as e:

           continue

       page.find("销售规格产品下拉框").click()

       # 第一次要新建产品

       if index == 0:

           page.find("销售规格产品新建产品").click()

           page.find("销售规格产品新-手动填写").click()

           page.find("销售规格产品信息-产品条形码").input("0000000000000")

           page.find("销售规格产品信息-品名").focus()

           sleep(1)

           page.find("销售规格产品信息-品名").input(productName)

           # 包装规格

           page.find("销售规格产品信息-包装规格输入框").focus()

           sleep(1)

           page.find("销售规格产品信息-包装规格输入框").input(sizeNum)

           page.find("销售规格产品信息-包装规格下拉框").click()

           page.find("销售规格产品信息-包装规格下拉框").click()

           for item in page.find_all("销售规格产品信息-包装规格下拉框内容"):

               if item.get_text() == sizeUnit:

                   item.click()

                   break

           # 净含量

           page.find("销售规格产品信息-单件净含量输入框").input(netContentNum)

           page.find("销售规格产品信息-单件净含量下拉框").click()

           page.find("销售规格产品信息-单件净含量下拉框").click()

           for item in page.find_all("销售规格产品信息-单件净含量下拉框内容"):

               if item.get_text() == netContentUnit:

                   item.click()

                   break

           # 上传图片

           page.find("销售规格产品信息-上传图片_3").hover()

           sleep(0.5)

           page.find("销售规格产品信息-上传图片_3").click()

           sleep(1)

           page.find("销售规格产品信息-上传图片内容").click()

           

           sleep(1)

           # 成分

           component2()    

           def fun1():

             page.find("销售规格产品信息-新增确认").click()

           retryFn(fun1)

       else:

           page.find("销售规格产品下拉框第一项").click()

           

       page.find("销售规格包含产品-确定").click()    

def split_number_unit_decimal(text):

   pattern = r'^(\d+\.?\d*)(\D+)$'

   match = re.match(pattern, text)

   if match:

       return match.groups()

   return [text]

def retryFn(fn):

   count = 0

   errorMsg = None

   for item in range(10):

       count += 1

       if count >= 6 : raise ValueError(errorMsg)

       try:

           fn()

           return True

       except Exception as e:

           sleep(1)

           errorMsg = e

           print('重试.....',e,count)

           continue


尝试过的方案

失败重试

收藏
全部回答1
最新
发布回答
回答