In this blog, we will discuss how to build a Youtube comment bot with the help of Python and the Selenium library. It is very helpful in growing your Youtube audience. This is because the Youtube comment bot increases interaction with other channels that return traffic to your channel. Apart from this, building a Youtube bot is very enjoyable to program. You will be able to learn many things. For instance, programming the bot to navigate the youtube site: open videos, click buttons, and enter comments. Lastly, in this tutorial, you can apply all selenium tricks to build bots for other websites.
The Youtube bot we are going to discuss will simulate being a regular user. For instance, it will open the browser, log in on youtube and enter any required search term. Then, it will open the resulting videos to leave a message. It will perform just like a normal user.
Please ensure that you are adding pauses when typing or clicking. This is very crucial to replicate what a user would do. When you are running a selenium bot against a bug site such as Youtube then it is necessary to simulate human behavior to avoid being detected and banned.
The advantage of using a bot is that, unlike humans, it will not get tired and will keep writing messages. It can go on until it gets detected so, let’s move on to the process of programming the bot to carry on this task.
1: Install Selenium
In this first section, we will be installing the selenium library. Which is the first step towards building a Python YouTube comment bot. You can easily install the library using the pip tool. Because this is a tool that manages all the python installed libraries.
Type the command given below from your terminal.
pip install selenium
To use the selenium you also need a driver, which is a browser. You can also use a browser that is already installed or download a driver. We would recommend you download a driver so that you won’t mess up your browser settings. If you want to download the browser you can download it through the link given below.
- Chrome: https://sites.google.com/a/chromium.org/chromedriver/downloads
- Edge: https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/
- Firefox: https://github.com/mozilla/geckodriver/releases
- Safari: https://webkit.org/blog/6900/webdriver-support-in-safari-10
Note: You have to make sure you remember where you save this file because you will need it in the following steps.
2: Open YouTube site
Now once you have installed the selenium and downloaded the driver file. It’s time to start adding some code to your bot. In this, the first step is opening the YouTube site: www.youtube.com. For opening YouTube with your bot, you will have to create a new python script and add the code given below.
from selenium import webdriver browser = webdriver.Chrome('./chromedriver') browser.get("https://www.youtube.com")
Note: Your chrome driver and your python script should be in the same folder.
3: Log in with a username and password
The following are the steps to signing to YouTube. It is a very simple task consists of 5 steps given below.
- Firstly, typing your email
- Then clicking next
- After that type the password
- Click next
- Lastly, clicking on confirm, although this step is not always needed.
In the code given below, we will provide it with a function that will carry out all the actions above in our bot browser. Now we will paste the code given below in our bot script between the import line, the first line, and line 3.
def login_with_username_and_password(browser, username, password): # Type email email_input = browser.find_element_by_css_selector('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) # Click next next_button = browser.find_elements_by_css_selector("button") time.sleep(2) next_button.click() time.sleep(2) # Type password password_input = browser.find_elements_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) # Click next next_button = browser.find_elements_by_css_selector("button") time.sleep(2) next_button.click() # Click Confirm confirm_button = browser.find_elements_by_css_selector("div[role=button]") time.sleep(2) if(len(confirm_button)>0): confirm_button.click()
Now the above code will first locate the target components by using the CSS selector.
n Selenium you can do so using two functions:
The main difference between the methods given above is that the first method returns a list containing all elements in the DOM document matching the CSS selector. But the second method only returns one value, the matching value.
Now once you have located our target component we can do several tasks like typing, sliding, and clicking. However, in this blog, we will mostly use click and type.
For using the click we will use the .click() method:
next_button = browser.find_elements_by_css_selector("button") next_button.click()
For type, we will use the
password_input = browser.find_elements_by_css_selector('input[type=password]') password_input.send_keys("any string can go here")
4: Enter a Search term
Now let’s talk about how to enter a search term into the search box. To do that you have to create a function that is passing in the browser and the search term. You can also paste this function in your python script under the previous function.
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)
As we discussed in the previous steps we will first locate the search input component in the DOM document. In this instance, we will use the method
.find_element_by_id("search") passing as argument the id of the target component:
search_input = browser.find_element_by_id("search")
After that enter the search term, letter by letter using the method
for letter in search_term: search_input.send_keys(letter) # Quick pause between letter to replicate human behavior wait_time = random.randint(0,1000)/1000 time.sleep(wait_time)
5: Click on a Video
Now once you have entered the search term, YouTube will display a list of video suggestions based on our search. All you need to do is select one of the videos and click. You could also select one random video, or alternatively, select the first one and then follow the list order.
thumbnails = browser.find_elements_by_css_selector("ytd-video-renderer") for index in range(1,6): thumbnails[index].click()
In the code given above, we used the
.find_elements_by_css_selector() method through which we can select all the videos listed on the page. And then, starting on the first one, click sequentially on each video.
6: Enter a Comment
Now the most important step entering a comment. After your browser opens the video page, you can insert your comments. To enter comments you need to do the following things.
- Firstly, move to the comment input field
- Then click on the component, so it gets the focus.
- After that type our comment
- Lastly, press the “Comment” button
Through the code, we defined how to perform all the actions above.
- Firstly, finding the target element in the case of comment input field.
- Then in the next step you will group the next actions like: type, click and move in an action chain by using the
- Lastly your bot will click confirm.
from selenium.webdriver.common.action_chains import ActionChains 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()
So, in order to mimic human behavior, you have inserted a millisecond pause after typing each letter that you can see in (lines 13 and 14).
7: Go back
Now your bot has opened a video and entered a comment. The next process is going back to the video list and then click on the next video. This we can easily achieve by clicking on the back button on the browser. Also, in Selenium you can visit the previous page using the code given below.
What this code will do is open the previous URL in the browser history.
8: Putting it all Together
What we have done till now is put some code in our bot script containing functions that will automatically log in to YouTube. Enter a search item in the search box and then enter a comment. Now the last step is putting it all together.
Now we will use a code that uses all functions created above and then open YouTube and log in. After that, you can use a set of search terms and for each search term, you enter your bot will: Enter a search term and then leave a comment on the first five videos listed. Then at last it will close the browser. Also, you can find the
.click_on_agree_and_signin() a function defined below.
browser = webdriver.Chrome('./chromedriver') browser.get("https://www.youtube.com") # Click Agree and Sing In click_on_agree_and_signin(browser) # Sign In login_with_username_and_password(browser, "your_username_here", "your_password_here") all_search_terms =['make money online','online marketing'] for search_term in all_search_terms: enter_search_term(browser, search_term) time.sleep(2) thumbnails = browser.find_elements_by_css_selector("ytd-video-renderer") for index in range(1,6): thumbnails[index].click() time.sleep(6) enter_comment(browser,"your comment here") browser.execute_script("window.history.go(-1)") thumbnails = browser.find_elements_by_css_selector("ytd-video-renderer") time.sleep(1) browser.close()
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('yt-button-renderer') time.sleep(6) # Wait longer so the message pops up while(len(signin_buttons)== 0): signin_buttons= browser.find_elements_by_css_selector('yt-button-renderer') time.sleep(1) signin_buttons.click()
Here we talked about how to build Youtube comment bot using selenium and python. We covered all the steps that a bot will perform from your login, then searching for a video and then entering a comment using selenium. Hope you find this information useful. Thank you for the read.