Home > Software design >  Message: no such element: Unable to locate element: {"method":"xpath","sele
Message: no such element: Unable to locate element: {"method":"xpath","sele

Time:02-18

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

CodePudding user response:

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.

CodePudding user response:

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())
  • Related