【问题标题】:Try except in python/selenium still throwing NoSuchElementException error在 python/selenium 中尝试 except 仍然抛出 NoSuchElementException 错误
【发布时间】:2018-01-10 01:21:28
【问题描述】:

我正在尝试在 python 中使用 selenium 捕获一些网站元素,并且我正在使用 try/except 以防在该特定页面中找不到特定元素。这一切都很好,但是脚本仍然会抛出 NoSuchElementException,即使我期待它并且已经告诉脚本以某种方式处理它或通过它。

我认为唯一可能是一个问题是这个 try/except 嵌套在另一个 try/except 中,所以我是这样的

for month in (month_start, month_end):

    for date in (date_start, date_end):

        try: #this is just a general try in case there is a breakdown in finding one of the elements
            driver.get(url)
            results = driver.find_elements_by_xpath("""//*[@class="results"]/div""")

            for result in results:
                sample_element = result.find_element_by_xpath("blah").text

                #seems to be where the problem starts
                specific_element = ""
                try:
                    #this is a specific element that I know may not exist
                    specific_element = result.find_element_by_xpath(""".//*[@class="specific"]/div""").text
                except NoSuchElementException:
                    specific_element = ""
                    #I have tried pass instead as well with no luck
                #throws an error here and won't continue if the specific element is not found
        except:
            #pretty much a copy of the above try with some changes

所以我通常认为我对 python 中的 try/except 函数有一个不错的理解,但这让我很头疼。“result in results”循环将愉快地继续,直到它找不到 specific_element 并抛出一个“selenium.common.exceptions.NoSuchElementException:消息:没有这样的元素:无法找到元素:”

如果嵌套在 try/except 中是整个问题的原因,您能否解释一下为什么会这样,并推荐一个我可以研究或实施的可能解决方案?或者也许我错过了一些基本的东西。

【问题讨论】:

  • 你导入 NoSuchElementException 吗?
  • 可能你使用了错误的NoSuchElementException。也许还有另一个NoSuchElementException 与您得到的名称相同。你能尝试显式导入from selenium.common.exceptions import NoSuchElementException吗?
  • @NiyamatUllah 是的,我有。它甚至似乎无法识别 try/except,并且在尝试失败后不会继续到 except。我也试过“除了:”,但仍然抛出同样的错误。
  • 你知道三重双引号的作用是什么吗?
  • @Rescis 据我了解,三重双引号与单双引号相同,但它可以包含换行符。对我个人而言,这只是在长行中突出显示字符串的一种简单方法。

标签: python selenium


【解决方案1】:

我不做 python,但如果是我,我会删除所有的 try/catch 并将它们替换为 find_elements_* 并检查空列表。例如

替换

specific_element = result.find_element_by_xpath(""".//*[@class="specific"]/div""").text

elements = result.find_elements_by_xpath(".//*[@class='specific']/div")
if elements
    specific_element = elements[0]

这基本上只是确保在访问之前找到任何元素,并且您可以避免所有尝试/捕获。我认为例外应该是例外的......很少见。它们不应该被期望或用作流量控制等。

【讨论】:

  • 没有必要添加文本我不做 python 但如果是我作为对任何答案的介绍。在 StackOverflow 上,它只不过是我们正在寻找好的答案的噪音
【解决方案2】:

您必须在这里注意几点:

  • 没有绝对必要try driver.get(url)
  • 问题在以下块中:

    try:
        specific_element = result.find_element_by_xpath(""".//*[@class="specific"]/div""").text
    

分析

try() 真正起作用之前,您已经尝试了一系列事件:

  • 评估

    result.find_element_by_xpath(""".//*[@class="specific"]/div""")
    
  • 下一个

    result.find_element_by_xpath(""".//*[@class="specific"]/div""").text
    
  • 下一个

    specific_element = result.find_element_by_xpath(""".//*[@class="specific"]/div""").text
    

所以,

  • 一旦 result.find_element_by_xpath(""".//*[@class="specific"]/div""") 失败,虽然 NoSuchElementException 被提升,try 仍然保持沉默。

解决方案

一个简单的解决方案是:

try:
    specific_element = result.find_element_by_xpath(""".//*[@class="specific"]/div""")
    specific_element_text = specific_element.text
    # other works

except NoSuchElementException:
    specific_element = ""
    # other works

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-12
    • 2022-01-07
    • 1970-01-01
    • 2023-02-09
    • 2019-09-29
    • 2020-04-03
    相关资源
    最近更新 更多