栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python报错:NoSuchElementException: no such element: Unable to locate element:

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python报错:NoSuchElementException: no such element: Unable to locate element:

用python、selenium爬取网页时出现一下错误:
NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="container"]/div/div/div[2]/h1"}
  (Session info: headless chrome=102.0.5005.63)
错误原因:

脚本执行到“xpath”这一行代码时,对应页面上的元素还没加载完成,因此报错。

解决方法:
#coding:utf-8
from selenium import webdriver
import selenium.webdriver.support.ui as ui

driver = webdriver.Chrome()
# driver = webdriver.Chrome(chrome_options=chrome_options)   # 也可用driver = webdriver.Chrome()
driver.get('http://www.xxxxx.com')
wait = ui.WebDriverWait(driver,9)
wait.until(lambda driver: driver.find_element_by_xpath("//span[text()='你好']"))
driver.find_element_by_xpath("//span[text()='你好']").click()
'''
import selenium.webdriver.support.ui as ui
wait = ui.WebDriverWait(driver,5)
wait.until(lambda driver: driver.find_element_by_xpath("//span[text()='你好']"))
其中这3行代码,表示会反复调用until中的函数,找到就返回,超过9秒就抛出Timeout异常:TimeoutException。
'''
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/934223.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号