My Selenium Webdriver script is returning an error that I don't understand

SkyeBoniwell

I am trying to use Seleniums WebDriverWait to connect to Hacker News

def connect_to_base(browser, page_number):
    base_url = f"https://news.ycombinator.com/news?p={page_number}"
    connection_attempts = 0
    while connection_attempts < 3:
        try:
            browser.get(base_url)
            # wait for table element with id = 'hnmain' to load
            # before returning True
            WebDriverWait(browser, 5).until(
                EC.presence_of_element_located((By.ID, "hnmain"))
            )
            return True
        except Exception as e:
            print(e)
            connection_attempts += 1
            print(f"Error connecting to {base_url}.")
            print(f"Attempt #{connection_attempts}.")
    return False
    

When I run the whole script, it errors in the above block of code with this error:

NameError: name 'hnmain' is not defined

I'm having trouble figuring out why it's saying 'hnmain' is undefined because 'hnmain' is just an element in the website.

Do I need to parse it another way?

Thanks!

Pujari Rajagonda

Here is the complete code I am giving you to try. I have executed it on firefox.

Few changes i have made to your code snippet as well.

import os
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium.webdriver.firefox.options import Options
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By


def connect_to_base(browser, page_number):
   base_url = 'https://news.ycombinator.com/news?p={}'.format(page_number)
   connection_attempts = 0
   while connection_attempts < 3:
       try:
           browser.get(base_url)
           # wait for table element with id = 'hnmain' to load
           # before returning True
           WebDriverWait(browser, 5).until(
               EC.presence_of_element_located((By.ID, "hnmain"))
           )
           return True
       except Exception as e:
           print(e)
           connection_attempts += 1
           print("Error connecting to {}".format(base_url))
           print("Attempt #{}".format(connection_attempts))
   return False


def GetDriver():
   if os.path.exists('C:/Program Files (x86)/Mozilla Firefox/firefox.exe'):
       binary = FirefoxBinary('C:/Program Files (x86)/Mozilla Firefox/firefox.exe')
   else:
       binary = FirefoxBinary('C:/Program Files/Mozilla Firefox/firefox.exe')
   options = Options()
   options.headless = False
   fp = webdriver.FirefoxProfile()
   fp.set_preference("browser.download.manager.showWhenStarting", False)
   fp.set_preference("browser.download.manager.showAlertOnComplete", False)
   fp.set_preference("browser.helperApps.neverAsk.openFile", "application/json")
   fp.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/json")
   return webdriver.Firefox(firefox_binary=binary, options=options,     
   service_log_path='c:/logs/gecko.log', firefox_profile=fp)


def launch_browser():
    connect_to_base(GetDriver(), 1)

 # Call the function
 launch_browser()

This code is working fine and giving you the expected results.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

My Selenium Webdriver script is returning an error that I don't understand

From Dev

I don't understand the error message int not iterable in my code

From Dev

Error 2025 that I absolutely don't understand

From Dev

Error with method arguments that I don't understand

From Dev

I don't understand why my script is not iterating through all string.split elements?

From Dev

I don't understand the results that's returning from elasticsearch/haystack

From Dev

Error: $injector:modulerr Module Error seems there is an injection error in my directive but I don't understand whats wrong in my code

From Dev

Haskell parse error in my xmonad.hs config that i don't understand?

From Dev

Don;t understand error

From Dev

I don't understand the NewNotImplementedException runtime error I'm getting

From Dev

I don't understand why I get this valgrind error

From Dev

I don't understand a valgrind error I'm getting

From Dev

C++ I don't understand my exceptions what() behavior

From Dev

Next is not defined, but I don't understand how to define it in my function

From Dev

C++ I don't understand my exceptions what() behavior

From Dev

i dont understand why my "if" command don't work as it should

From Dev

Flash as3 Error 1180 that I don't understand

From Dev

Don't understand why do I get a permission denied error

From Dev

Some beginner error I don't understand in python

From Dev

Getting syntax error in sudoers file I don't understand

From Dev

Android error on Attempt to invoke virtual method; I don't understand it

From Dev

I don't understand the error shown by the VS2013 compiler

From Dev

I don't understand how to fix the syntax error

From Dev

Getting a really strange logic error which I don't understand

From Dev

Receiving error I don't understand using Smartadmin Bootstrap Datatables

From Dev

I don't understand why it says ArrayOutOfBound error

From Dev

Don't understand Tweepy error

From Dev

Don't understand the error 1119

From Dev

Don't understand error message

Related Related

  1. 1

    My Selenium Webdriver script is returning an error that I don't understand

  2. 2

    I don't understand the error message int not iterable in my code

  3. 3

    Error 2025 that I absolutely don't understand

  4. 4

    Error with method arguments that I don't understand

  5. 5

    I don't understand why my script is not iterating through all string.split elements?

  6. 6

    I don't understand the results that's returning from elasticsearch/haystack

  7. 7

    Error: $injector:modulerr Module Error seems there is an injection error in my directive but I don't understand whats wrong in my code

  8. 8

    Haskell parse error in my xmonad.hs config that i don't understand?

  9. 9

    Don;t understand error

  10. 10

    I don't understand the NewNotImplementedException runtime error I'm getting

  11. 11

    I don't understand why I get this valgrind error

  12. 12

    I don't understand a valgrind error I'm getting

  13. 13

    C++ I don't understand my exceptions what() behavior

  14. 14

    Next is not defined, but I don't understand how to define it in my function

  15. 15

    C++ I don't understand my exceptions what() behavior

  16. 16

    i dont understand why my "if" command don't work as it should

  17. 17

    Flash as3 Error 1180 that I don't understand

  18. 18

    Don't understand why do I get a permission denied error

  19. 19

    Some beginner error I don't understand in python

  20. 20

    Getting syntax error in sudoers file I don't understand

  21. 21

    Android error on Attempt to invoke virtual method; I don't understand it

  22. 22

    I don't understand the error shown by the VS2013 compiler

  23. 23

    I don't understand how to fix the syntax error

  24. 24

    Getting a really strange logic error which I don't understand

  25. 25

    Receiving error I don't understand using Smartadmin Bootstrap Datatables

  26. 26

    I don't understand why it says ArrayOutOfBound error

  27. 27

    Don't understand Tweepy error

  28. 28

    Don't understand the error 1119

  29. 29

    Don't understand error message

HotTag

Archive