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

相关推荐
测试19982 小时前
Jmeter接口自动化生成测试报告html格式详解
自动化测试·python·测试工具·jmeter·职场和发展·测试用例·接口测试
大汉堡玩测试19 小时前
langfuse测试实战(一): 配置一个简单的项目快速落地
python·测试工具
B2_Proxy1 天前
Selenium配置代理IP的完整指南:从启动参数到认证扩展
selenium·测试工具
weixin_440730501 天前
Playwright介绍、特点、小例子还有与传统selenium的区别
selenium·测试工具·playwright
chuntian_tester2 天前
测试中的提示词工程
人工智能·测试工具·ai
测试狗科研平台2 天前
电池材料表征、电芯制备与性能测试--测试狗先进能源中试服务
科技·测试工具·材料工程
啊阿狸不会拉杆3 天前
《计算机网络-自顶向下方法》2.8 小结、课后习题和 Wireshark 实验 读书笔记
计算机网络·测试工具·wireshark·因特网
PS测3 天前
Linux压力测试工具stress,能精确控制 CPU、内存、IO 负载类型和持续时间
测试工具
2601_962387823 天前
web自动化测试实战教程【selenium/unittest/pytest】【共193课
selenium·pytest·devops·web自动化测试·unittest