SkyeBoniwell
SkyeBoniwell

Reputation: 7092

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

I am execute following code 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 above script, I am getting the following 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 ID of a webelement element in the website.

Do I need to parse it another way?

Upvotes: 2

Views: 40

Answers (1)

Humble_PrOgRaMeR
Humble_PrOgRaMeR

Reputation: 741

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.

Upvotes: 2

Related Questions