我對編碼比較陌生,對堆疊跟蹤錯誤有點不熟悉。我收到了這個錯誤,但我不確定我要修復代碼的哪一部分?錯誤是由于 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[@]')):
# 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[@]')):
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[@hljs-string">"]"}
(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
uj5u.com熱心網友回復:
這個錯誤資訊...
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 DOM 中唯一標識元素,則沒有元素的相關 HTML 將難以解決。
此外,雖然您正在使用,chrome=95.0.4638.69但您的ChromeDriver版本與 Chrome 瀏覽器版本不同步。因此,您會看到Backtrace。
解決方案
確保這件事:
- ChromeDriver更新到當前ChromeDriver v95.0級別。
uj5u.com熱心網友回復:
Selenium 無法定位元素,
noslot_back_button = browser.find_element(By.CSS_SELECTOR, 'input[name="btnBack"]')
很可能是打字錯誤,或者您可能忘記命名 css 選擇器。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/355817.html
標籤:Python 硒 硒铬驱动器 堆栈跟踪 没有这样的元素例外
