Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Syntax Error : I can't identify what's wrong!
#1
# 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:
            browser.get(url)

            print("What is your Google email?")
            email = input("")
            print("What is your Google password?")
            password = input("")

            email_input = browser.find_element_by_type("email")
            email_input.send_keys(email, Keys.ENTER)
            password_input = browser.find_element_by_type("password")
            password_input.send_keys(password, Keys.ENTER)

        except:
            print(" ")
            print(self.error)

# fetches input URL from user
    def fetch_input(self):
        print("- ")
        print("What video do you want to comment on?")
        print("- ")
        self.url = 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()
What seems to be wrong here? I can't figure it out.
buran wrote May-31-2019, 04:18 PM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Quote
#2
Please, post the entire traceback that you get.
Take a time to read What to include in a post
You should get also IndentationError (e.g. look at lines 54-64)
Quote
#3
(May-31-2019, 04:18 PM)buran Wrote: Please, post the entire traceback that you get.
Take a time to read What to include in a post
You should get also IndentationError (e.g. look at lines 54-64)

Yes, my apologies regarding the tags,

Right off the bat, I get an error from the login method (the except block is ran)
When I insert the link, I get a syntax error.

Here is the console when the program is ran:
Error:
[!] There was an error! - What video do you want to comment on? - Enter URL: https://www.youtube.com/watch?v=SEpmYLu-CCA Traceback (most recent call last): File "/Users/carsonrhodes/PycharmProjects/dyl/venv/Main.py", line 97, in <module> Commenter.fetch_input() File "/Users/carsonrhodes/PycharmProjects/dyl/venv/Main.py", line 58, in fetch_input self.url = input("Enter URL: ") #sample url : https://www.youtube.com/watch?v=SEpmYLu-CCA File "<string>", line 1 https://www.youtube.com/watch?v=SEpmYLu-CCA ^ SyntaxError: invalid syntax Process finished with exit code 1
Quote
#4
Which python version are you using?
buran likes this post
Quote
#5
this looks strange:
Error:
File "<string>", line 1 https://www.youtube.com/watch?v=SEpmYLu-CCA
for start remove the try/except block from the login() method to fix this error. when using all-catch exception you mask what is going on
also don't use Commenter as instance variable, use commenter
Quote
#6
ah, Yoriz has a point
Quote
#7
(May-31-2019, 04:43 PM)Yoriz Wrote: Which python version are you using?
I am using Python 2.7
Quote
#8
Quote:self.url = input("Enter URL: ")
python2.x is raw_input()
Either use python3.x or change all input() to raw_input()
Quote
#9
(Jun-10-2019, 01:36 AM)metulburr Wrote:
Quote:self.url = input("Enter URL: ")
python2.x is raw_input() Either use python3.x or change all input() to raw_input()

Hey

I changed all my inputs to raw_inputs, thank you for this tip.

I still have my except blocks going off and I'm not quite sure why.
My login method doesn't seem to work at all.

# 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:
            browser.get(url)
            time.sleep(4)
            print("What is your Google email?")
            email = raw_input("")
            print("What is your Google password?")
            password = raw_input("")
            time.sleep(3)
            email_input_box = browser.find_element_by_type("email")
            email_input_box.send_keys(email, Keys.ENTER)
            password_input_box = browser.find_element_by_type("password")
            password_input_box.send_keys(password, Keys.ENTER)

        except:
            print(" ")
            print(self.error)

# 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()

Quote
#10
Change your try/except blocks, so the exception that happens is actually displayed
try:
    ...
    ...
except Exception as exception:
    print(exception)
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Syntax error - precedence Rev2k 3 136 Jan-03-2020, 08:36 PM
Last Post: Rev2k
  Keep getting "invalid syntax error" jpringle1 3 247 Dec-25-2019, 04:43 PM
Last Post: Gribouillis
  'invalid syntax' error rix 3 156 Dec-12-2019, 11:14 PM
Last Post: stullis
  Need help to identify Mersenne Primes, I do need a search pattern. Pleiades 0 129 Dec-03-2019, 11:05 PM
Last Post: Pleiades
  Syntax Error (elif statement) Kanashi 0 138 Nov-20-2019, 11:29 PM
Last Post: Kanashi
  Invalid Syntax error mustfirst 9 550 Nov-11-2019, 10:07 AM
Last Post: deep_logic
  Syntax Error megearhart4 2 185 Oct-20-2019, 05:27 PM
Last Post: ichabod801
  python gives wrong string length and wrong character thienson30 2 213 Oct-15-2019, 08:54 PM
Last Post: Gribouillis
  kivy - packaging touchtracer syntax error cram 2 224 Oct-14-2019, 08:30 PM
Last Post: cram
  sqlite3.OperationalError: near "%": syntax error Linuxdesire 2 964 Oct-13-2019, 02:54 AM
Last Post: Linuxdesire

Forum Jump:


Users browsing this thread: 1 Guest(s)