0

Why is my exception not kicking in and catching the error. I know it cant go up to 35 because there is no 35, but it should peform my exception if this is the case or not? Thanks for answares in advance.

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


options = webdriver.ChromeOptions()
options.add_experimental_option('excludeSwitches', ['enable-logging'])
driver = webdriver.Chrome(options=options)
website = "https://langerball.de/"
driver.get(website)

land = "DEUTSCHLAND"
x = driver.find_element(By.XPATH, "(//a[(text()='%s')])" %land)
x.click()


def maxspieltag(): 
    i = 1
    while i in range(100):
        xpath_t = ("//b[text()='%s']" %i)
        try:
            driver.find_element(By.XPATH, xpath_t)
        except NoSuchElementException:
            i -= 1
            break
        i +=1
    return i

print(maxspieltag())

I added a picture because it worked before now it does not want to and for better explenation:

enter image description here

Fuorb
  • 27
  • 6

3 Answers3

0

If you analyze the DOM Tree using Chrome Development Tool:

bundesliga

You will observe,

<b>34</b>

is the last element within the table and the following locator strategies:

//b[text()='35']

doesn't identifies any element within the HTML DOM. Hence you see the error.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

Needed to untick the option Raised Exceptions.

Fuorb
  • 27
  • 6
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 23 '22 at 09:40
-1

In my humble opinion you might have not saved the file before executing.

I've executed your code and it's working for me.

EDIT:

Retried the entire code without making any changes before hand and here is the full error:

root@92fa00e3f091:/opt/code# python seleniumtest.py
Traceback (most recent call last):
  File "/opt/code/seleniumtest.py", line 27, in maxspieltag
    driver.find_element(By.XPATH, xpath_t)
  File "/usr/local/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 976, in find_element
    return self.execute(Command.FIND_ELEMENT, {
  File "/usr/local/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/usr/local/lib/python3.9/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//b[text()='35']"}
  (Session info: chrome=96.0.4664.45)


During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/opt/code/seleniumtest.py", line 35, in <module>
    print(maxspieltag())
  File "/opt/code/seleniumtest.py", line 29, in maxspieltag
    letzter_spieltag
NameError: name 'letzter_spieltag' is not defined

If you look at the bottom of the exception you'll find the real exception you are looking for, uninitialized "letzter_spieltag" variable. Commenting it out and retrying works:

root@92fa00e3f091:/opt/code# python seleniumtest.py
34

Here is fixed code:

def maxspieltag():
    i = 1
    while i in range(100):
        xpath_t = ("//b[text()='%s']" %i)
        try:
            driver.find_element(By.XPATH, xpath_t)
        except NoSuchElementException:
            # letzter_spieltag
            i -= 1
            break
        i +=1
    return i

print(maxspieltag())
  • Yes I should have delete letzter_spieltag becaus i change the code, I saved it and it shows me an error, I will now add a picture, can you look over it? – Fuorb Feb 17 '22 at 16:02
  • Could you print the exception module? Replace `except NoSuchElementException:` with `except Exception as ex:` and print the exception path with `print(type(ex))` – Arslan Sohail Bano Feb 17 '22 at 18:32
  • No cant print it, but sometimes the consol shows me the result of 34. – Fuorb Feb 18 '22 at 10:47