2

The Situation:

Firstly I would like to say that am new to selenium and decided to pick it up to practice some python. I am currently following a tutorial online and decided to make a youtube bot.

The Code:

    from selenium import webdriver
    from selenium.webdriver.chrome.webdriver import WebDriver
    import time
    import random
    
    from selenium.webdriver.common.action_chains import ActionChains
    from selenium.webdriver.common.keys import Keys
    
    
    def login_with_username_and_password(browser, username, password):
        # FILL UP THE LOGIN FORM
        email_input = browser.find_elements('input[type=email]')
    
        email = username
        for letter in email:
            email_input.send_keys(letter)
            wait_time = random.randint(0,1000)/1000
            time.sleep(wait_time)
    
        next_button = browser.find_elements_by_css_selector("button")
        time.sleep(2)
        next_button[2].click()
        time.sleep(2)
    
        password_input = browser.find_element_by_css_selector('input[type=password]')
        password = password
        for letter in password:
            password_input.send_keys(letter)
            wait_time = random.randint(0,1000)/1000
            time.sleep(wait_time)
    
        next_button = browser.find_elements_by_css_selector("button")
        time.sleep(2)
        next_button[1].click()
    
        confirm_button = browser.find_elements_by_css_selector("div[role=button]")
        time.sleep(2)
        if(len(confirm_button)>0):
            confirm_button[1].click()
    
    def click_on_agree_and_signin(browser):
        # agree_button= browser.find_element_by_css_selector('button')
        # time.sleep(2)
        # agree_button.click()
    
        signin_buttons= browser.find_elements_by_css_selector(".signin")
        time.sleep(6) # Wait longer so the message pops up
        while(len(signin_buttons)== 0):
            signin_buttons= browser.find_elements_by_css_selector(".signin")
            time.sleep(1)
    
        signin_buttons[0].click()
    
    def enter_search_term(browser,search_term):
        # Enter text on the search term
        search_input = browser.find_element_by_id("search")
        for letter in search_term:
            search_input.send_keys(letter)
            wait_time = random.randint(0,1000)/1000
            time.sleep(wait_time)
    
        search_input.send_keys(Keys.ENTER)
    
    def enter_comment(browser, comment):
        comment_input = browser.find_element_by_css_selector("ytd-comment-simplebox-renderer")
    
        entering_comment_actions = ActionChains(browser)
    
        entering_comment_actions.move_to_element(comment_input)
        entering_comment_actions.click()
    
        for letter in comment:
            entering_comment_actions.send_keys(letter)
            wait_time = random.randint(0,1000)/1000
            entering_comment_actions.pause(wait_time)
    
        entering_comment_actions.perform()
    
        time.sleep(1)
    
        send_comment_button = browser.find_element_by_id("submit-button")
        send_comment_button.click()
    
    ###########################################
    #             BOT STARTS HERE             #
    ###########################################
    
    
    driver=webdriver.Chrome()
    driver.maximize_window
    driver.get("https://www.youtube.com/")
    all_search_terms = ['online marketing']
    
    # Click Agree and Sing In
    click_on_agree_and_signin(driver)
    
    # Sign In
    login_with_username_and_password(driver, "[email protected]", "-1qa2ws3ed4rf-")
    for search_term in all_search_terms:
        enter_search_term(driver, search_term)
        time.sleep(2)
    
        thumbnails = driver.find_element_by_css_selector("ytd-video-renderer")
    
        for index in range(1, 6):
            thumbnails[index].click()
            time.sleep(6)
            enter_comment(driver, "love it")
            driver.execute_script("window.history.go(-1)")
            thumbnails = driver.find_element_by_css_selector("ytd-video-renderer")
    time.sleep(1)
    driver.close()

The Problem: When Running this code it produces an error related to the find_element_by_css_selector method. Most frequently during the sign in phase as shown here.

The Question:

Can anybody explain what is going on here and where I am going wrong, as well as how I can fix this please.

3
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. Commented Oct 25, 2021 at 15:25
  • always put code, data and full error message as text (not screenshot, not link) in question (not in comment). Commented Oct 26, 2021 at 5:54
  • it is warning that find_elements_by_... are deprecated and probably you have to use find_elements(BY...., ). It seems they change this in the newest version of Selenium because I have version 3.141.0 and it still can use find_elements_by_.... You can check your version with print(selenium.__version__) Commented Oct 26, 2021 at 5:57

1 Answer 1

4

First: it is only warning, not error.


In my version 3.141.0 I can use both methods

from selenium import webdriver

driver = webdriver.Chrome() # Firefox()

driver.find_elements_by_css_selector(...)
driver.find_elements_by_xpath(...)
# etc.

and

from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome() # Firefox()

driver.find_elements(By.CSS_SELECTOR, ...)
driver.find_elements(By.XPATH, ...)
# etc.

but it seems they plan to remove functions find_elements_by_... in the future (in versions 4.x) and now find_elements_by_... still works but it shows warning that you should use second method find_elements(By.CSS_SELECTOR, ...).

You could use module warnings to hide these warnings but better start using only second method.


BTW:

In source code for find_elements_by_css_selector you can see it runs warning.warn(...) and next it runs find_elements(By.CSS_SELECTOR, ...)


To check what version you use

import selenium

print(selenium.__version__)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.