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

相关推荐
aiweker4 小时前
Selenium 使用指南:从入门到精通
python·selenium·测试工具
愈努力俞幸运3 天前
python selenium 用法教程
python·selenium
Future_yzx3 天前
Selenium 浏览器操作与使用技巧——详细解析(Java版)
java·selenium·测试工具
Algorithm-0073 天前
【Postman 接口测试】接口用例设计与评审
测试工具·postman
武陵悭臾4 天前
网络爬虫学习:应用selenium获取Edge浏览器版本号,自动下载对应版本msedgedriver,确保Edge浏览器顺利打开。
学习·selenium·edge·deepseek·winreg·zipfile
Future_yzx4 天前
selenium自动化测试框架——面试题整理
java·selenium·intellij-idea
hunter2062065 天前
ubuntu无法上网的解决办法
网络·测试工具·ubuntu
代码的乐趣5 天前
支持selenium的chrome driver更新到132.0.6834.110
chrome·python·selenium
霍格沃兹测试开发学社测试人社区5 天前
性能测试丨JVM 性能数据采集
软件测试·jvm·测试开发·测试工具
Algorithm-0075 天前
【Postman接口测试】Postman的全局变量和环境变量设置
测试工具·postman