阅读123 2024-05-16 15:25 采纳率: 100%
浏览 4
已结题

Python By定位元素

Python By定位元素,遇到问题:

定位一个输入框,无id、name、_TEXT等,尝试通过By.CLASS_NAME、By.TAG_NAME定位元素然后输入,定位时HTML中有多个,尝试使用

input_elements = driver.find_elements(By.CLASS_NAME,"nui-ipt-input")
if input_elements:
    first_input = input_elements[2]
    first_input.send_keys("测试")

upload_images = driver.find_element(By.TAG_NAME,"input")
if upload_images:
    images_input = upload_images[10]
    images_input.click().send_keys("C:\上传文件\测试.jpg")

可以定位并输入,但是后续步骤中再次定位一个输入框,然后输入时,使用By.CLASS_NAME或By.TAG_NAME,都失败,如下:

upload_images = driver.find_element(By.TAG_NAME,"input")
if upload_images:
    images_input = upload_images[10]
    images_input.click().send_keys("C:\上传文件\测试.jpg")

或

upload_images1 = driver.find_element(By.CLASS_NAME,"nui-ipt-input")
if len(upload_images1) >= 5:
    upload_images1[4].send_keys("C:\上传文件\测试.jpg")
else:
    print('没有足够的元素供选择')

都失败了,提示:

    images_input = upload_images[10]
                   ~~~~~~~~~~~~~^^^^
TypeError: 'WebElement' object is not subscriptable


     if len(upload_images1) >= 5:
       ^^^^^^^^^^^^^^^^^^^
TypeError: object of type 'WebElement' has no len()

各位帮忙看下。

  • 写回答

3条回答 默认 最新

  • 阿里嘎多学长 2024-05-16 15:25
    关注

    以下内容由CHATGPT及阿里嘎多学长共同生成、有用望采纳:


    您想要解决的问题

    您提问的目的是解决在使用Python的Selenium库进行网页自动化测试时,遇到的元素定位和操作的问题。具体来说,您在尝试通过By.CLASS_NAMEBy.TAG_NAME定位网页中的输入框,并进行输入操作时,遇到了无法正确定位和操作元素的问题。

    问题出现原因

    1. TypeError: 'WebElement' object is not subscriptable:这个错误表明您尝试对一个WebElement对象进行了索引操作(即使用方括号[]),但WebElement对象是不可索引的。您应该使用find_elements方法来获取一个元素列表,然后才可以进行索引操作。
    2. TypeError: object of type 'WebElement' has no len():这个错误表明您尝试对一个WebElement对象调用了len()函数,但WebElement对象没有这个属性。len()函数用于获取容器类型(如列表、元组等)的长度,而WebElement对象是单个元素,不是容器。

    问题解决方案

    要解决这个问题,您需要使用find_elements方法来获取一个元素列表,然后才能对列表进行索引和长度检查。以下是修改后的代码示例:

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    
    # 假设 driver 是您已经初始化的 WebDriver 实例
    
    # 定位多个输入框
    input_elements = driver.find_elements(By.CLASS_NAME, "nui-ipt-input")
    if input_elements:
        first_input = input_elements[2]  # 现在 first_input 是一个 WebElement 列表中的对象
        first_input.send_keys("测试")
    
    # 定位文件上传输入框
    upload_images = driver.find_elements(By.TAG_NAME, "input")
    if upload_images:
        images_input = upload_images[10]  # 现在 images_input 是一个 WebElement 列表中的对象
        images_input.click()
        images_input.send_keys("C:\\上传文件\\测试.jpg")  # 注意路径中的空格需要转义
    
    # 确保路径中的空格被正确处理
    file_path = "C:\\上传文件\\测试.jpg"
    images_input.send_keys(file_path)
    

    提供代码

    上述代码已经提供了解决问题的方法。确保您的Selenium环境已经正确设置,包括WebDriver的安装和路径配置。

    代码运行方式

    运行代码需要Python环境,以及安装了Selenium库和对应的WebDriver。代码可以直接在一个Python脚本文件中运行,或者在一个IDE(如PyCharm)中运行。

    代码预期运行结果

    如果代码正确执行,那么它将成功定位到第11个input元素(因为索引是从0开始的),并尝试将其作为文件上传控件进行操作。注意,实际的网页元素结构和属性可能会影响代码的行为。

    推荐相关链接

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 5月24日
  • 已采纳回答 5月16日
  • 创建了问题 5月16日

悬赏问题

  • ¥15 有可能用平板通过拓展坞来烧录程序吗(keil5的那种)
  • ¥15 网络分析设施点无法识别
  • ¥15 状态图的并发态问题咨询
  • ¥15 PFC3D,plot
  • ¥15 VAE模型编程报错无法解决
  • ¥100 基于SVM的信息粒化时序回归预测,有偿求解!
  • ¥15 物体组批优化问题-数学建模求解答
  • ¥350 麦克风声源定位坐标不准
  • ¥15 apifox与swagger使用
  • ¥15 egg异步请求返回404的问题