首页 > 解决方案 > 即使尝试除了代码块,错误也会不断出现

问题描述

我一直在尝试实现这种自动化,从而刷新页面(通过按下后退按钮并按下搜索按钮),直到搜索页面中出现插槽。所以我创建了一个for循环,按下webelement搜索按钮并按下返回按钮返回到上一页。底部多余的代码是为了确保程序预订插槽,但是当我收到错误时,它通常不会到达那些代码行。

然而,在运行程序一段时间后,一旦我找到一个插槽,并且后退按钮变得过时,我得到错误 NoSuchElementException,找不到元素按钮返回(参考 qn 的底部),即使我已经指出了程序的内容当无法在 except 列下找到后退按钮时应该这样做 - 然后继续到 except 块并选择网页上可用的插槽

旁注:程序能够运行一段时间,刷新页面,所以我确定我已经为后退按钮输入了正确的元素路径。但是,我真的无法弄清楚我的 except 块有什么问题,以至于我的程序不会自动到达那里并在错误弹出时按照指示预订插槽,而是在控制台中返回错误

    no_slot = True
while no_slot == 1:
    for i in range(60):
        search_button = browser.find_element(By.NAME, "btnSearch")
        search_button.click()
        try:
            noslot_back_button = browser.find_element(By.CSS_SELECTOR, 'input[name="btnBack"]')
        except NoSuchElementException:
            # Proceed with booking
            slot_radio_info = browser.find_element(By.NAME, 'slot')
            slot_id = slot_radio_info.get_attribute('id')
            radio_button = browser.find_element(By.XPATH, '//input[@id="' + str(slot_id) + '"]')
            radio_button.click()
            slot_submit_button = browser.find_element(By.CSS_SELECTOR, 'input[value="Submit"]')
            slot_submit_button.click()
            double_cfm_button = browser.find_element(By.CSS_SELECTOR, 'input[value="Confirm"]')
            double_cfm_button.click()
            no_slot = False

这是控制台中收到的错误:

    Traceback (most recent call last):
  File "C:/Users/Jonathan/PycharmProjects/Riddle/BBDC_fullauto.py", line 81, in <module>
    noslot_back_button = browser.find_element(By.CSS_SELECTOR, 'input[name="btnBack"]')
  File "C:\Users\Jonathan\venvs\automation\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 1238, in find_element
    return self.execute(Command.FIND_ELEMENT, {
  File "C:\Users\Jonathan\venvs\automation\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 418, in execute
    self.error_handler.check_response(response)
  File "C:\Users\Jonathan\venvs\automation\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 243, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"input[name="btnBack"]"}
  (Session info: chrome=95.0.4638.69)
Stacktrace:
Backtrace:
    Ordinal0 [0x00CC3AB3+2505395]
    Ordinal0 [0x00C5AE41+2076225]
    Ordinal0 [0x00B62498+1057944]
    Ordinal0 [0x00B8CB74+1231732]
    Ordinal0 [0x00BB6D92+1404306]
    Ordinal0 [0x00BA5A2A+1333802]
    Ordinal0 [0x00BB5168+1397096]
    Ordinal0 [0x00BA58BB+1333435]
    Ordinal0 [0x00B823E4+1188836]
    Ordinal0 [0x00B8323F+1192511]
    GetHandleVerifier [0x00E4CB36+1554566]
    GetHandleVerifier [0x00EF4A0C+2242396]
    GetHandleVerifier [0x00D50E0B+523099]
    GetHandleVerifier [0x00D4FEB0+519168]
    Ordinal0 [0x00C602FD+2097917]
    Ordinal0 [0x00C64388+2114440]
    Ordinal0 [0x00C644C2+2114754]
    Ordinal0 [0x00C6E041+2154561]
    BaseThreadInitThunk [0x7592FA29+25]
    RtlGetAppContainerNamedObjectPath [0x774E7A9E+286]
    RtlGetAppContainerNamedObjectPath [0x774E7A6E+238]
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "C:\Users\Jonathan\PyCharm\plugins\python-ce\helpers\pydev\pydevd.py", line 1483, in _exec
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "C:\Users\Jonathan\PyCharm\plugins\python-ce\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "C:/Users/Jonathan/PycharmProjects/Riddle/BBDC_fullauto.py", line 89, in <module>
    slot_submit_button.click()
  File "C:\Users\Jonathan\venvs\automation\lib\site-packages\selenium\webdriver\remote\webelement.py", line 80, in click
    self._execute(Command.CLICK_ELEMENT)
  File "C:\Users\Jonathan\venvs\automation\lib\site-packages\selenium\webdriver\remote\webelement.py", line 693, in _execute
    return self._parent.execute(command, params)
  File "C:\Users\Jonathan\venvs\automation\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 418, in execute
    self.error_handler.check_response(response)
  File "C:\Users\Jonathan\venvs\automation\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 243, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element <input type="button" name="" value="Submit" class="btn" onclick="postBooking(this.form, 'b-TPDSBookingConfirm.asp?limit=TPDS', '', 'Please select a slot to book.')" onmouseover="blueBtnOver(this)" onmouseout="blueBtnOut(this)"> is not clickable at point (118, 330). Other element would receive the click: <td align="right" class="bluetxtbold">...</td>
  (Session info: chrome=95.0.4638.69)
Stacktrace:
Backtrace:
    Ordinal0 [0x00CC3AB3+2505395]
    Ordinal0 [0x00C5AE41+2076225]
    Ordinal0 [0x00B62498+1057944]
    Ordinal0 [0x00B92C09+1256457]
    Ordinal0 [0x00B90F48+1249096]
    Ordinal0 [0x00B8ED0D+1240333]
    Ordinal0 [0x00B8DB68+1235816]
    Ordinal0 [0x00B83857+1194071]
    Ordinal0 [0x00BA59F3+1333747]
    Ordinal0 [0x00B83676+1193590]
    Ordinal0 [0x00BA5ADA+1333978]
    Ordinal0 [0x00BB5168+1397096]
    Ordinal0 [0x00BA58BB+1333435]
    Ordinal0 [0x00B823E4+1188836]
    Ordinal0 [0x00B8323F+1192511]
    GetHandleVerifier [0x00E4CB36+1554566]
    GetHandleVerifier [0x00EF4A0C+2242396]
    GetHandleVerifier [0x00D50E0B+523099]
    GetHandleVerifier [0x00D4FEB0+519168]
    Ordinal0 [0x00C602FD+2097917]
    Ordinal0 [0x00C64388+2114440]
    Ordinal0 [0x00C644C2+2114754]
    Ordinal0 [0x00C6E041+2154561]
    BaseThreadInitThunk [0x7592FA29+25]
    RtlGetAppContainerNamedObjectPath [0x774E7A9E+286]
    RtlGetAppContainerNamedObjectPath [0x774E7A6E+238]
python-BaseException

标签: pythonseleniumerror-handlingtry-exceptnosuchelementexception

解决方案


这不是答案,但由于字符限制,我无法将其放入评论中。

继续评论:

@ToangarashiWoon 堆栈跟踪不仅仅意味着错误消息,但在这种情况下是的。因此,查看堆栈跟踪您可以看到实际错误发生在slot_submit_button.click().

如果您阅读堆栈跟踪,您会看到该行

noslot_back_button = browser.find_element(By.CSS_SELECTOR, 'input[name="btnBack"]')

抛出一个错误,这是意料之中的,但是如果您继续阅读堆栈跟踪,您会看到以下行:

During handling of the above exception, another exception occurred:

这告诉你的是except块中有一个异常。更具体地说:

 File "C:/Users/Jonathan/PycharmProjects/Riddle/BBDC_fullauto.py", line 89, in <module>
    slot_submit_button.click() 

带有错误消息:

selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element <input type="button" name="" value="Submit" class="btn" onclick="postBooking(this.form, 'b-TPDSBookingConfirm.asp?limit=TPDS', '', 'Please select a slot to book.')" onmouseover="blueBtnOver(this)" onmouseout="blueBtnOut(this)"> is not clickable at point (118, 330). Other element would receive the click: <td align="right" class="bluetxtbold">...</td>

根据此错误消息,问题似乎是您无法单击它,因为td元素阻止了该按钮。


推荐阅读