python - Selenium NoSuchElementException:没有这样的元素:无法找到元素
问题描述
我正在尝试使用 python 进行 Web 自动化的以下代码:
from selenium import webdriver
import time
web = webdriver.Chrome()
base_url = ""
web.get(base_url)
time.sleep(2)
team = web.find_element_by_xpath('// * [@id = "ReportViewerControl_xyz123_xyz456_dvalue"]/option[4]')
team.click()
但是,我收到以下异常:
NoSuchElementException: no such element: Unable to locate element
解决方案
在开始使用之前,您需要先了解一些 HTML Selenium
。你得到NoSuchElementException
是因为没有找到该元素。您可以使用此导入来捕获此错误:
from selenium.common.exceptions import NoSuchElementException
然后像这样写一个try
/except
块:
try:
# your code
except NoSuchElementException:
# this block of code will execute when you get this exception
推荐阅读
- java - Java,DateTimeFormatter 不考虑 ZoneDateTime 的时区
- tmux - tmux - 为每个命令强制重复前缀键
- r - 使用 R 有效地计算列中单词列表的出现次数
- java - CLion 插件开发:如何向现有方法添加方法调用
- c# - 从 c# 中的泛型类返回列表但列表(协变)没有在主类中更新?
- docker - 如何加载下载的图像,通过 ansible playbook 在 docker 容器中运行它
- javascript - 如何在Javascript正则表达式中提取引号之间的文本
- mysql - 分组计数?
- php - PHP Apache 模块 mod_evasive 不仅被 IP 阻塞
- ruby-on-rails - 正则表达式电子邮件:ruby 中电子邮件的数字 + 格式