【问题标题】:Using a for loop with a try except block but still gives me an error使用带有 try except 块的 for 循环,但仍然给我一个错误
【发布时间】:2021-11-07 06:13:24
【问题描述】:

我是新手,正在尝试使用 python 和 selenium 创建一个机器人项目。 在我的机器人的这一部分中,我正在尝试创建一个 for 循环,以便程序刷新页面几次(使用搜索和后退按钮),然后继续执行我已排除的 while 循环的其余部分与我遇到的错误无关。

所以,我想做的是找到一个可以预订的位置。如果找到了一个插槽,我们就废弃了后退按钮,网页中将不再有这样的元素。所以,我利用这个功能摆脱了刷新循环,而是转到了 except 块,这有望让我摆脱两个循环(for 循环和 while 循环)。但是,我认为我仍然无法退出 for 循环,因为我看到此错误弹出,即使我已尝试处理 NoSuchElementException 错误。

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:
            no_slot = False
            break
        else:
            noslot_back_button.click()

这是我收到的错误:

  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

【问题讨论】:

  • 请记住,Chrome 是在一个完全独立的进程中运行的。它不是 Python 的一部分。当它得到一个异常时,它会做自己的异常处理。
  • selenium.common.exceptions.ElementClickInterceptedException 似乎还有一个重叠元素:消息:元素点击拦截:元素 在点 (118, 330) 不可点击。其他元素会收到点击: ... 。您可以通过 driver.execute_script 单击元素来避免此错误。
  • 嗨@ArundeepChohan,让我解释一下我对收到的错误的解释。那么在 try except 块由于后退按钮不再可点击而移动到 except 块期间,在 except 块中发生了另一个错误,导致这个重叠错误?另外,您能否详细说明 driver.execute_script 在我的情况下对我有什么帮助?谢谢

标签: python selenium selenium-webdriver try-except nosuchelementexception


【解决方案1】:

您是否忘记导入异常?

from selenium.common.exceptions import NoSuchElementException

【讨论】:

  • 嗨,我确实导入了异常
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多