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 天前
Python接口测试之requests库安装和导入
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·接口测试
祉猷并茂,雯华若锦3 天前
Selenium与Playwright元素定位终极对决
selenium·测试工具
小白学大数据4 天前
长周期爬虫的数据一致性:断点续爬 + 事务回滚保障采集质量
开发语言·爬虫·测试工具
小绫网络安全4 天前
Wireshark抓包完全入门教程:从零开始掌握网络分析
arm开发·测试工具·wireshark
梦想不只是梦与想5 天前
鸿蒙 测试工具:DevEco Testing(一)
测试工具·harmonyos·testing
ClouGence5 天前
Selenium 写不动了?这个工具录一次就能跑 Web 自动化
前端·selenium·测试
我的xiaodoujiao5 天前
快速学习Python基础知识详细图文教程17--类型注解和断点调试
开发语言·python·学习·测试工具
泽众云测试6 天前
泽众软件与日联科技达成合作,TestOne自动化测试平台助力搭建工业检测装备数字化品控防线
功能测试·测试工具·自动化