T
T
Tula Electronic Samovar2021-12-22 14:29:10
Python
Tula Electronic Samovar, 2021-12-22 14:29:10

Errors when using Selenium in Python when searching for an element by class name, what's wrong?

Elementary search in HTML element page

# импорт библиотек

from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
driver.get('https://www.pochta.ru/tracking#30000052425166')
x_path = "//*[@class='TrackingCardHistory__Layout-sc-zdvopc-0 enJRDv']"
element  = driver.find_element(By.XPATH,  x_path)
# Поиск тегов по имени
print(element.text())


Gives a bunch of errors, and in service modules...
D:\_proekt\pythonProject\chromepost\venv\Scripts\python.exe D:/_proekt/pythonProject/chromepost/main.py
Traceback (most recent call last):
  File "D:\_proekt\pythonProject\chromepost\main.py", line 22, in <module>
    element  = driver.find_element(By.XPATH,  x_path)
  File "D:\_proekt\pythonProject\chromepost\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 1244, in find_element
    return self.execute(Command.FIND_ELEMENT, {
  File "D:\_proekt\pythonProject\chromepost\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 424, in execute
    self.error_handler.check_response(response)
  File "D:\_proekt\pythonProject\chromepost\venv\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 247, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@class='TrackingCardHistory__Layout-sc-zdvopc-0 enJRDv']"}
  (Session info: chrome=96.0.4664.110)
Stacktrace:
Backtrace:
  Ordinal0 [0x01046903+2517251]
  Ordinal0 [0x00FDF8E1+2095329]
  Ordinal0 [0x00EE2848+1058888]
  Ordinal0 [0x00F0D448+1233992]
  Ordinal0 [0x00F0D63B+1234491]
  Ordinal0 [0x00F37812+1406994]
  Ordinal0 [0x00F2650A+1336586]
  Ordinal0 [0x00F35BBF+1399743]
  Ordinal0 [0x00F2639B+1336219]
  Ordinal0 [0x00F027A7+1189799]
  Ordinal0 [0x00F03609+1193481]
  GetHandleVerifier [0x011D5904+1577972]
  GetHandleVerifier [0x01280B97+2279047]
  GetHandleVerifier [0x010D6D09+534521]
  GetHandleVerifier [0x010D5DB9+530601]
  Ordinal0 [0x00FE4FF9+2117625]
  Ordinal0 [0x00FE98A8+2136232]
  Ordinal0 [0x00FE99E2+2136546]
  Ordinal0 [0x00FF3541+2176321]
  BaseThreadInitThunk [0x76B56359+25]
  RtlGetAppContainerNamedObjectPath [0x77607B74+228]
  RtlGetAppContainerNamedObjectPath [0x77607B44+180]
Process finished with exit code 1


What's wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan, 2021-12-22
@vcomp71

You have a clear mistake
selenium.common.exceptions.NoSuchElementException
. Better wrap everything in a try.

from selenium.common.exceptions import NoSuchElementException

try:
     element  = driver.find_element(By.CLASS_NAME,  class_name)
     print(element.text)
except NoSuchElementException:
     print('Элемент не найден')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question