Selenium使用注意事项:

find_element 和 find_elements 的区别

WebDriver和WebElement的区别

问题:

会遇到报错:

复制代码
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="1"]"}

NoSuchElementException 的意思就是在当前的网页上找不到该元素, 就是找不到id为1的元素。

分析原因:

可以用sleep等待时间来解决

python 复制代码
from selenium import webdriver
from selenium.webdriver.common.by import By

wd = webdriver.Chrome()

wd.get('https://www.byhy.net/_files/stock1.html')

element.send_keys('通讯\n')

element = wd.find_element(By.ID, 'go')
element.click()

import time

while True:
    try:
        element=wd.find_element(By.ID,'1')
        print(element.txt)
        break
    except:
        time.sleep(1)

wd.quit()

但是这样的解决方案存在问题:

Selenium的Webdriver对象有个方法叫 implicitly_wait ,可以称之为隐式等待 ,或者全局等待 。该方法接受一个参数, 用来指定最大等待时长。

python 复制代码
wd.implicitly_wait(10)

最后

python 复制代码
from selenium import webdriver
from selenium.webdriver.common.by import By

wd = webdriver.Chrome()
wd.implicitly_wait(10)

wd.get('https://www.byhy.net/_files/stock1.html')

element = wd.find_element(By.ID, 'kw')

element.send_keys('通讯\n')


# 返回页面 ID为1 的元素
element = wd.find_element(By.ID,'1')

print(element.text)

iframe元素非常的特殊, 在html语法中,frame 元素 或者iframe元素的内部 会包含一个被嵌入的另一份html文档。导致如果直接读取.plant就没有办法读取到,就必须切换操作范围 到被嵌入的文档中,需要加入

相关推荐
川石课堂软件测试10 分钟前
requests接口自动化测试
数据库·python·功能测试·测试工具·单元测试·grafana·prometheus
emilyhu20262 小时前
测试工具fiddler抓包后Response的乱码问题解决方式汇总
测试工具·fiddler
我的xiaodoujiao2 小时前
API 接口自动化测试详细图文教程学习系列12--Requests模块4--测试实践操作
python·学习·测试工具·pytest
LT10157974444 小时前
2026年云测试平台选型指南:全场景真机与自动化技术实测
测试工具·自动化
流氓也是种气质 _Cookie12 小时前
Wireshark在Windows XP系统上的安装与使用指南
windows·测试工具·wireshark
ZLG_zhiyuan21 小时前
竞逐“芯”赛道:CAN分析仪如何成为芯片行业高效测试工具?
测试工具
Lugas Luo21 小时前
DVR 存储工具深度分析报告 (测试与产品视角)
linux·嵌入式硬件·测试工具
LT10157974441 天前
2026年云测试平台选型指南:技术能力与场景匹配全解析
测试工具
IT19952 天前
Wireshark笔记-对AI连接标准MCP抓包分析
笔记·测试工具·wireshark