Python Forum

Full Version: Syntax Error : I can't identify what's wrong!
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
(Jun-10-2019, 05:29 AM)Yoriz Wrote: [ -> ]Change your try/except blocks, so the exception that happens is actually displayed
try:
    ...
    ...
except Exception as exception:
    print(exception)

Thank you for that, I will write my exceptions like this in the future.

using this code,
# imports
import random
import time
from selenium import webdriver

class Commenter:
# declares variables for commentor script
    def __init__(self):
        self.error = "[!] There was an error!"
        self.url =  " "
        self.browser = webdriver.Chrome(executable_path = '/Users/carsonrhodes/Desktop/Chrome Driver/chromedriver')
        self.comment_box = " "

# logs user into browser so that they may comment on a video.
    def login(self):

        # URL for google login
        url = "https://accounts.google.com/servicelogin"

        try:
            self.browser.get(url)
            time.sleep(4)
            print("What is your Google email?")
            email = raw_input("")
            print("What is your Google password?")
            password = raw_input("")
            email_input_box = self.browser.find_element_by_id("identifierId")
            email_input_box.send_keys(email, self.Keys.ENTER)
            time.sleep(3)
            password_input_box = self.browser.find_element_by_class_name("password")
            password_input_box.send_keys(password, self.Keys.ENTER)

        except Exception as exception:
            print(self.error)
            print(exception)

# fetches input URL from user
    def fetch_input(self):
        print("- ")
        print("What video do you want to comment on?")
        print("- ")
        self.url = raw_input("Enter URL: ") #sample url : https://www.youtube.com/watch?v=SEpmYLu-CCA

# finds comment box so that the script can comment.
    def find_comment_box(self):
        try:
            self.browser.get(self.url)
            self.comment_box = self.browser.find_element_by_id(contenteditable-root)
        except:
            print(self.error)

# generates a comment from CSV file.
    def generate_comment(self):
         comments = []
         try:
            with open('Comments for Bot .csv') as csv_file:
                csv_reader = csv.reader(csv_file, delimiter='\n')
                for row in csv_reader:
                 comments.append(row)

            return(random.choice(comments))
         except:
            print(" ")
            print(self.error)

# fetches comment from generating script
    def fetch_comment(self):
        comment = self.generate_comment()
        print("[!] Comment generated successfully!")
        return(comment)

# enters comment into comment box on YouTube
    def enter_comment(self):
        self.comment_box.send_keys(self.fetch_comment())


Commenter = Commenter() #creates class (cannot call methods without calling class)

# code starts here:
Commenter.login()
Commenter.fetch_input()
Commenter.find_comment_box()
Commenter.generate_comment()
I am receiving this error:
"
[!] There was an error!
Commenter instance has no attribute 'Keys'
"
You are calling self.keys when keys has not been assigned to anything in class Commenter.
Try/except blocks are more for catching particular errors that are expected to happen and then writing code to deal with it.
A catch all exception allows code to carry on regardless which is not often the desired outcome.
Pages: 1 2