【问题标题】:Selenium NoSuchElementException: no such element: Unable to locate elementSelenium NoSuchElementException:没有这样的元素:无法找到元素
【发布时间】:2021-09-17 05:36:12
【问题描述】:

我正在尝试使用 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 来指导您。
  • 请澄清您的具体问题或提供其他详细信息以准确突出您的需求。正如目前所写的那样,很难准确地说出你在问什么。

标签: python selenium webdriver


【解决方案1】:

在开始使用Selenium 之前,您需要先了解一些 HTML。你得到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

【讨论】:

    猜你喜欢
    • 2017-09-15
    • 1970-01-01
    • 1970-01-01
    • 2020-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-09
    • 1970-01-01
    相关资源
    最近更新 更多