0

I’ve got this part code in my script:

elements = driver.find_elements(By.TAG_NAME, 'p')
for e in elements:
if e.text not in elenco:
    print(e.text)
    elenco.append(e.text)
    if ("Denis" in e.text):                   
            print("Find Denis")

Many times the script break in these rows with this message:

selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document

Other times in the other rows (follow) with a similar message like:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {“method”:“css selector”,“selector”:“[id=“submitmessage”]”}

It seems like it finds a problem when the webpage change within these rows. Can someone please help me to fix it? Thanks

ClaudiaR
  • 3,108
  • 2
  • 13
  • 27

1 Answers1

0

For the first error message:

In short, the message is saying that the element you're looking for is no longer in the DOM, or it changed.

You may be able to try something like below to help

# Add some items to your import statements, to help handle exceptions
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import StaleElementReferenceException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions

# Then, add some code to help handle exceptions
target_Tag_Name = "p"

ignored_exceptions=(NoSuchElementException,StaleElementReferenceException)
your_element = WebDriverWait(your_driver, some_timeout,ignored_exceptions=ignored_exceptions)\
                        .until(expected_conditions.presence_of_element_located((By.TAG_NAME, target_Tag_Name)))

# Then place your code below
elements = driver.find_elements(By.TAG_NAME, 'p')
for e in elements:
if e.text not in elenco:
    print(e.text)
    elenco.append(e.text)
    if ("Denis" in e.text):                   
            print("Find Denis")

The link below may offer a bit more context for the first error message:

StaleElementReferenceException on Python Selenium

For the second error message:

In short, the message is saying that the element can't be found on the page.

This could happen because your script is running too quickly, and the page doesn't have time to load fully.

There are a few ways to help address that - the thread below may offer some more context

selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element:

Note that the code snippet above the your_element variable includes a WebDriverWait statement, which may slow the execution of your script enough to allow the page to load (Basically the code snippet above might solve the second error message)

jrynes
  • 187
  • 1
  • 9
  • Thanks but it gives me this error message "'NoneType' object has no attribute 'find_element') with reference to this part of code" your_element = WebDriverWait(My_driver, 1, ignored_exceptions=ignored_exceptions).until(ec.presence_of_element_located(By.TAG_NAME)) – Denis Dal Soler Oct 20 '22 at 08:37