Forums

selenium.common.exceptions.NoSuchElementException Error

Hi, first of all, I apologize to all of you because my English is not good, but I know how to run this code on my own computer, it gives such an error when I type pythonanywhere and I couldn't figure out why.

from pyvirtualdisplay import Display
from selenium import webdriver

with Display():
    browser = webdriver.Firefox()
    try:
        browser.get('https://www.facebook.com/')
        element = browser.find_element_by_xpath("//*[@id='reg_pages_msg']/a")
        print(element.text)
    finally:
        browser.quit()

Traceback (most recent call last):
  File "/home/seleniumusername1/selenium1/manage.py", line 8, in <module>
  element = browser.find_element_by_xpath("//*[@id='reg_pages_msg']/a")
  File "/usr/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 293, in                      
 find_element_by_xpath
     return self.find_element(by=By.XPATH, value=xpath)
  File "/usr/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 750, in find_element
    return self.execute(Command.FIND_ELEMENT, {
  File "/usr/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 236, in execute
    self.error_handler.check_response(response)
  File "/usr/lib/python3.8/site-packages/selenium/webdriver/remote/errorhandler.py", line 192, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: {"method":"xpath","selector":"//*[@id='reg_pages_msg']/a"}

Thanks for your help!

The element that you're looking for is not in the page that you're getting back from facebook. Examine the html of the response that you're getting to start to work out why the expected element is not there.

Good afternoon! I'm trying to run a script on your server. The script works with Selenium. And everything was fine at first, but during the program the code crashes and gives the error No Such Element. At the same time, it is strange that the code always throws an error at different stages. Sometimes it throws an error right away, sometimes after several iterations. In general, with the same input data, it works every other time. At the same time, I am sure that the problem is precisely in the use of Selenium. Since with normal get requests everything is fine. Tell me, please, how can I solve this problem? I watched this topic on the forum. Also added all the necessary options “—disable-dev-shm-usage”, “—no-sandbox" and “—disable-gpu". However, the error still occurs

I see that you posted in another forum thread about this, so have replied there.