使用 Selenium 和 ChromeDriver 时,堆栈跟踪错误中的 Backtrace: Ordinal0 [0x00723AB3+2505395] 意味着什么

问题描述 投票:0回答:2

我对编码比较陌生,对堆栈跟踪错误有点不熟悉。 我收到此错误,但我不确定要修复代码的哪一部分?错误来了吗 是因为 NoSuchElementException 还是因为超时错误?

我认为在我的代码中我已经处理了程序将如何面对 nosuchelement 异常。基本上,我的代码搜索可用的插槽,并在插槽可用时禁用后退按钮。但是,我遇到了其他类型的错误,例如当网页闲置时间过长时连接超时。因此,我现在的任务就是过滤掉我面临的不同问题,以便我能够分别处理这些问题。我已添加预期条件作为过滤器来区分我遇到的错误

代码:

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:
            if EC.presence_of_element_located(browser.find_element(By.XPATH, '//td[@class="title"]')):
                # Proceed with booking (make sure that it is at the booking page (implementation)
                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"]')
                WebDriverWait(EC.element_to_be_clickable(slot_submit_button))
                slot_submit_button.click()
                double_cfm_button = browser.find_element(By.CSS_SELECTOR, 'input[value="Confirm"]')
                double_cfm_button.click()
            elif not EC.title_contains('BBDC: Booking For TP Driving Simulator Lesson'):
                print("Connection timed out or something else happened")
            else:
                print("unaccounted condition")

收到错误:

Traceback (most recent call last):
  File "C:/Users/Jonathan/PycharmProjects/Riddle/BBDC.py", line 83, 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 [0x00723AB3+2505395]
    Ordinal0 [0x006BAE41+2076225]
    Ordinal0 [0x005C2498+1057944]
    Ordinal0 [0x005ECB74+1231732]
    Ordinal0 [0x00616D92+1404306]
    Ordinal0 [0x00605A2A+1333802]
    Ordinal0 [0x00615168+1397096]
    Ordinal0 [0x006058BB+1333435]
    Ordinal0 [0x005E23E4+1188836]
    Ordinal0 [0x005E323F+1192511]
    GetHandleVerifier [0x008ACB36+1554566]
    GetHandleVerifier [0x00954A0C+2242396]
    GetHandleVerifier [0x007B0E0B+523099]
    GetHandleVerifier [0x007AFEB0+519168]
    Ordinal0 [0x006C02FD+2097917]
    Ordinal0 [0x006C4388+2114440]
    Ordinal0 [0x006C44C2+2114754]
    Ordinal0 [0x006CE041+2154561]
    BaseThreadInitThunk [0x7739FA29+25]
    RtlGetAppContainerNamedObjectPath [0x77987A9E+286]
    RtlGetAppContainerNamedObjectPath [0x77987A6E+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.py", line 86, in <module>
    if EC.presence_of_element_located(browser.find_element(By.XPATH, '//td[@class="title"]')):
  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":"xpath","selector":"//td[@class="title"]"}
  (Session info: chrome=95.0.4638.69)
Stacktrace:
Backtrace:
    Ordinal0 [0x00723AB3+2505395]
    Ordinal0 [0x006BAE41+2076225]
    Ordinal0 [0x005C2498+1057944]
    Ordinal0 [0x005ECB74+1231732]
    Ordinal0 [0x00616D92+1404306]
    Ordinal0 [0x00605A2A+1333802]
    Ordinal0 [0x00615168+1397096]
    Ordinal0 [0x006058BB+1333435]
    Ordinal0 [0x005E23E4+1188836]
    Ordinal0 [0x005E323F+1192511]
    GetHandleVerifier [0x008ACB36+1554566]
    GetHandleVerifier [0x00954A0C+2242396]
    GetHandleVerifier [0x007B0E0B+523099]
    GetHandleVerifier [0x007AFEB0+519168]
    Ordinal0 [0x006C02FD+2097917]
    Ordinal0 [0x006C4388+2114440]
    Ordinal0 [0x006C44C2+2114754]
    Ordinal0 [0x006CE041+2154561]
    BaseThreadInitThunk [0x7739FA29+25]
    RtlGetAppContainerNamedObjectPath [0x77987A9E+286]
    RtlGetAppContainerNamedObjectPath [0x77987A6E+238]

python-BaseException
Traceback (most recent call last):
  File "C:\Users\Jonathan\PyCharm\plugins\python-ce\helpers\pydev\_pydevd_bundle\pydevd_comm.py", line 292, in _on_run
    r = self.sock.recv(1024)
TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

Process finished with exit code 1
python selenium selenium-chromedriver stack-trace nosuchelementexception
2个回答
0
投票

Selenium 无法定位元素

noslot_back_button = browser.find_element(By.CSS_SELECTOR, 'input[name="btnBack"]')
很可能是拼写错误或者您忘记命名 css 选择器。


0
投票

这个错误信息...

Traceback (most recent call last):
.
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 [0x00723AB3+2505395]
    Ordinal0 [0x006BAE41+2076225]
    Ordinal0 [0x005C2498+1057944]

...意味着在定位元素时引发了 NoSuchElementException


解决 NoSuchElementException

如果没有元素的相关 HTML,如果定位器策略HTML DOM 中唯一标识该元素,将很难解决。

此外,虽然您使用的是

chrome=95.0.4638.69
,但您的 ChromeDriver 版本与 Chrome 浏览器版本不同步。因此你会看到Backtrace


解决方案

确保:

© www.soinside.com 2019 - 2024. All rights reserved.