Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Python Chromedriver

  • Thread starter Deleted member 1442
  • Start date
D

Deleted member 1442

Guest
Hi, I'm making a YouTube comment bot. (pls don't look at the comments list, I beg u)
I get a error/output of:

Bash:
DevTools listening on ws://127.0.0.1:49883/devtools/browser/10038dfc-6cb3-447d-9b53-c6185133b8f4
Traceback (most recent call last):
  File "C:\Users\admin\Desktop\CommentBot\bot.py", line 147, in <module>
    driver = youtube_login(email, password)
  File "C:\Users\admin\Desktop\CommentBot\bot.py", line 32, in youtube_login
    driver = webdriver.Chrome(options=op, executable_path= 'chromedriver.exe')
  File "C:\Users\admin\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 76, in __init__
    RemoteWebDriver.__init__(
  File "C:\Users\admin\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 157, in __init__
    self.start_session(capabilities, browser_profile)
  File "C:\Users\admin\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 252, in start_session
.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Users\admin\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.SessionNotCreatedException: Message: session not created: This version of ChromeDriver only supports Chrome version 86
Current browser version is 88.0.4324.182 with binary path C:\Program Files\Google\Chrome\Application\chrome.exe

Code:
Python:
import os, time, random, spintax, requests, config
import numpy as np
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from random import randint, randrange
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException

PROXY = "3.88.169.225:80"

def stop(n):
    time.sleep(randint(2, n))

#login bot===================================================================================================
def youtube_login(email,password):

    op = webdriver.ChromeOptions()
    # op.binary_location = os.environ.get("GOOGLE_CHROME_BIN")
    # op.add_argument('--headless')
    op.add_argument('--disable-dev-shm-usage')
    # op.add_argument('--no-sandbox')
    op.add_argument('--disable-gpu')
    # op.add_argument("--window-size=1920,1080")
    op.add_argument("disable-infobars")
    op.add_argument("--disable-extensions")
    # op.add_argument('--proxy-server=%s' % PROXY)
    # op.add_argument("--proxy-bypass-list=*")
    driver = webdriver.Chrome(options=op, executable_path= 'chromedriver.exe')
    driver.execute_script("document.body.style.zoom='80%'")
    driver.get('https://accounts.google.com/ServiceLogin?service=youtube&uilel=3&passive=true&continue=https%3A%2F%2Fwww.youtube.com%2Fsignin%3Faction_handle_signin%3Dtrue%26app%3Ddesktop%26hl%3Den%26next%3Dhttps%253A%252F%252Fwww.youtube.com%252F&hl=en&ec=65620')

    print("=============================================================================================================")
    print("Google Login")

    #finding email field and putting our email on it
    email_field = driver.find_element_by_xpath('//*[@id="identifierId"]')
    email_field.send_keys(email)
    driver.find_element_by_id("identifierNext").click()
    stop(5)
    print("email - done")

    #finding pass field and putting our pass on it
    find_pass_field = (By.XPATH, '//*[@id="password"]/div[1]/div/div[1]/input')
    WebDriverWait(driver, 50).until(EC.presence_of_element_located(find_pass_field))
    pass_field = driver.find_element(*find_pass_field)
    WebDriverWait(driver, 50).until(EC.element_to_be_clickable(find_pass_field))
    pass_field.send_keys(password)
    driver.find_element_by_id("passwordNext").click()
    stop(5)
    print("password - done")
    WebDriverWait(driver, 200).until(EC.presence_of_element_located((By.CSS_SELECTOR, "ytd-masthead button#avatar-btn")))
    print("Successfully login")
    print("============================================================================================================")

    return driver
#==============================================================================================================



#comment bot===================================================================================================
def comment_page(driver,urls,comment):

    if len( urls ) == 0:
        print("============================================================================================================")
        print ('Finished keyword jumping to next one...')
        return []
    
    #gettin a video link from the list
    url = urls.pop()
    
    driver.get(url)
    print("Video url:" + url)
    driver.implicitly_wait(1)

    #checking if video is unavailable
    if not check_exists_by_xpath(driver,'//*[@id="movie_player"]'):
        print("skipped")
        return comment_page(driver, urls, random_comment())


    
    time.sleep(2)
    #Reminder: You can add like function by uncommenting 4 lines below
    # like_button = EC.presence_of_element_located(By.XPATH, '/html/body/ytd-app/div/ytd-page-manager/ytd-watch-flexy/div[4]/div[1]/div/div[5]/div[2]/ytd-video-primary-info-renderer/div/div/div[3]/div/ytd-menu-renderer/div/ytd-toggle-button-renderer[1]/a/yt-icon-button/button/yt-icon')
    # WebDriverWait(driver, 50).until(EC.element_to_be_clickable(like_button)).click()
    # print('Liked')
    # time.sleep(1)
    driver.execute_script("window.scrollTo(0, window.scrollY + 500)")
    time.sleep(1)

    #checking if comments are disabled
    if not check_exists_by_xpath(driver,'//*[@id="simple-box"]/ytd-comment-simplebox-renderer'):
        print("skiped")
        return comment_page(driver, urls, random_comment())

    #checking if video is a livestream
    if check_exists_by_xpath(driver,'//*[@id="contents"]/ytd-message-renderer'):
        print("skiped")
        return comment_page(driver, urls, random_comment())

    #finding comment box and submiting our comment on it
    comment_box = EC.presence_of_element_located((By.CSS_SELECTOR, '#placeholder-area'))
    WebDriverWait(driver, 4).until(comment_box)
    comment_box1 = driver.find_element_by_css_selector('#placeholder-area')
    ActionChains(driver).move_to_element(comment_box1).click(comment_box1).perform()
    add_comment_onit = driver.find_element_by_css_selector('#contenteditable-root')
    add_comment_onit.send_keys(comment) 
    driver.find_element_by_css_selector('#submit-button').click()
    print("done")

    stop(5)

    return comment_page(driver, urls, random_comment())
#==============================================================================================================


#comment section
def random_comment():
# You can edit these lines if you want to add more comments===================================
    comments = [
        'Good Video! Like this comment or your family will be taken hostage.',
        'Good Job on the Video! Like this comment or your family will be taken hostage.'
    ]
# =============================================================================================
    r = np.random.randint(0, len(comments))

    return comments[r]
 
def check_exists_by_xpath(driver,xpath):
    try:
        driver.find_element_by_xpath(xpath)
    except NoSuchElementException:
        return False

    return True


#running bot------------------------------------------------------------------------------------
if __name__ == '__main__':
    email = config.email
    password = config.password

    driver = youtube_login(email, password)

    while True:
        key = driver.find_element_by_name('search_query')

        #get keyword list and extract each key
        with open('keywords.txt', 'r') as f:
            keywords = [line.strip() for line in f]
            random_keyword = random.choice(keywords)
            keys = spintax.spin(random_keyword)

            #send keyword in the search box
            for char in keys:
                key.send_keys(char)
        
        time.sleep(1)

        #click search icon
        driver.find_element_by_css_selector('#search-icon-legacy > yt-icon').click()
        time.sleep(3)
        #click filter button to filter the videos for the recently uploaded, you can remove or edit this option
        driver.find_element_by_css_selector('#container > ytd-toggle-button-renderer > a').click()
        time.sleep(3)

        #filtering for last hour
        driver.find_element_by_xpath("(//yt-formatted-string[@class='style-scope ytd-search-filter-renderer'])[1]").click()
        time.sleep(3)
        
        #grabbing videos titles
        for i in range(2):
            ActionChains(driver).send_keys(Keys.END).perform()
            time.sleep(1)
        titles = driver.find_elements_by_xpath('//*[@id="video-title"]')

        urls = []

        #getting url from href attribute in title
        for i in titles:
            if i.get_attribute('href') != None:
                urls.append(i.get_attribute('href'))
            else:
                continue
    
        #checking if we have links or not
        if urls == []:
            print("There is not videos for this keyword at the moment")
        else:
            comment_page(driver,urls,random_comment())
 
Last edited by a moderator:
Updated Chromedriver.exe got another error:
Bash:
[7836:16968:0221/134548.880:ERROR:device_event_log_impl.cc(211)] [13:45:48.887] Bluetooth: bluetooth_adapter_winrt.cc:1072 Getting Default Adapter failed.
Traceback (most recent call last):
  File "C:\Users\admin\Desktop\CommentBot\bot.py", line 147, in <module>
    driver = youtube_login(email, password)
  File "C:\Users\admin\Desktop\CommentBot\bot.py", line 48, in youtube_login
    WebDriverWait(driver, 50).until(EC.presence_of_element_located(find_pass_field))
  File "C:\Users\admin\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:

image:
https://gyazo.com/20cbc9df22653e2056c9a1a7d0e83ed5
 
from the first set of errors.. if you follow the stack down the line...


File "C:\Users\admin\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.SessionNotCreatedException: Message: session not created: This version of ChromeDriver only supports Chrome version 86
Current browser version is 88.0.4324.182 with binary path C:\Program Files\Google\Chrome\Application\chrome.exe
 
I already have selenium 1.26.3, stable release of Chromedriver v88.

This browser or app may not be secure. Learn more
Try using a different browser. If you’re already using a supported browser, you can refresh your screen and try again to sign in.


This is what chrome says, I think I need to make it "secure" then I can fix the other errors.
(edit: I also have javascript enabled.)
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom