当前位置: 首页> 游戏> 评测 > Selenium使用注意事项:

Selenium使用注意事项:

时间:2025/7/9 5:46:40来源:https://blog.csdn.net/weixin_71113035/article/details/140304918 浏览次数:0次

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等待时间来解决

from selenium import webdriver
from selenium.webdriver.common.by import Bywd = 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 timewhile True:try:element=wd.find_element(By.ID,'1')print(element.txt)breakexcept:time.sleep(1)wd.quit()

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

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

wd.implicitly_wait(10)

最后

from selenium import webdriver
from selenium.webdriver.common.by import Bywd = 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)

关键字:Selenium使用注意事项:

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: