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就没有办法读取到,就必须切换操作范围 到被嵌入的文档中,需要加入

相关推荐
测试界的小码农21 小时前
【 AI Agent 软件测试实战(05):任务规划与状态管理,让 Agent 学会拆解复杂测试任务】
人工智能·测试工具
和粒科技3 天前
热重分析仪TG 309 Libra Classic
测试工具
ClouGence4 天前
不搭 Selenium 或 Playwright,怎么测试公司的内网业务?
selenium·测试
螺丝钉tina9 天前
[Playwright] [02]常用命令与调试:Debug UI、Mode和 Trace Viewer
开发语言·javascript·测试工具·ui·自动化
梧桐16810 天前
14天、31个缺陷、28个测试脚本—用 Claude Code 完成第三方软件测试
人工智能·测试工具
mewowaBc10 天前
postman使用细则
测试工具·postman·偏差
光影62710 天前
Unittest单元测试基础速记
测试工具·单元测试·json·测试覆盖率
xie8810180211 天前
正则里的点号不匹配换行:多行模式的开关
运维·服务器·测试工具
2601_9679873712 天前
Python在自动化测试中有哪些应用?
python自动化测试·selenium·appium·pytest·性能测试
东舟技术12 天前
从人工目检到AI视觉:智能座舱瞬态故障的自动化捕获与溯源
功能测试·测试工具·自动化