我对编码相对陌生,对堆栈跟踪错误也有点陌生。我收到了这个错误,但我不确定我应该修复代码的哪一部分?错误来了吗是因为NoSuchElementException,还是由于超时错误?
我认为在我的代码中,我已经处理了程序将如何面对nosuchlement异常。基本上,我的代码会搜索可用的插槽,并在插槽可用时禁用后退按钮。然而,我遇到了其他类型的错误,例如当网页空闲太久时连接超时。因此,我现在的任务是过滤掉我面临的不同问题,以便我能够分别处理这些问题。我添加了预期条件作为过滤器,以区分我在中面临的错误
代码:
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:UsersJonathanvenvsautomationlibsite-packagesseleniumwebdriverremotewebdriver.py", line 1238, in find_element
return self.execute(Command.FIND_ELEMENT, {
File "C:UsersJonathanvenvsautomationlibsite-packagesseleniumwebdriverremotewebdriver.py", line 418, in execute
self.error_handler.check_response(response)
File "C:UsersJonathanvenvsautomationlibsite-packagesseleniumwebdriverremoteerrorhandler.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:UsersJonathanPyCharmpluginspython-cehelperspydevpydevd.py", line 1483, in _exec
pydev_imports.execfile(file, globals, locals) # execute the script
File "C:UsersJonathanPyCharmpluginspython-cehelperspydev_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:UsersJonathanvenvsautomationlibsite-packagesseleniumwebdriverremotewebdriver.py", line 1238, in find_element
return self.execute(Command.FIND_ELEMENT, {
File "C:UsersJonathanvenvsautomationlibsite-packagesseleniumwebdriverremotewebdriver.py", line 418, in execute
self.error_handler.check_response(response)
File "C:UsersJonathanvenvsautomationlibsite-packagesseleniumwebdriverremoteerrorhandler.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:UsersJonathanPyCharmpluginspython-cehelperspydev_pydevd_bundlepydevd_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
Selenium无法定位元素noslot_back_button = browser.find_element(By.CSS_SELECTOR, 'input[name="btnBack"]')
很可能是打字错误,或者您忘记命名css选择器。
此错误消息。。。
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,那么如果Locator Strategy在HTMLDOM中唯一地标识元素,那么将很难进行寻址。
此外,尽管您正在使用chrome=95.0.4638.69
,但您的ChromeDriver版本与Chrome浏览器版本不同步。因此,您可以看到回溯。
解决方案
确保:
- ChromeDriver更新为当前ChromeDriverv95.0级别