HOW wanting to be the BEST in english online class concludes to RICK-ROLLING my friends | Python Selenium

HOW wanting to be the BEST in english online class concludes to RICK-ROLLING my friends | Python Selenium

·

7 min read

Intro to my idea

We all know about the pandemic. It is currently one of the top topics the news all around the world talk about (besides considering there is war in Europe...). During the Covid-19 Pandemic the schools in Germany closed and switched to online classes and in general online school. Besides the fact that the infrastructure was way to bad to do so and that the system just wasn't organised enough to function correctly, it was quite a fun time exactly because the time was like a year-long vacation at home. But I don't wanna talk about that in this post. So since we switched to online class and got back to school we have like a hybrid online and not online class-system (every class exists online but every teacher chooses himself if they use the online-system).

So concluding to what I wanna say: We have in our english class a skill system where you can get XP by contributing. And what I saw was that you would get 5xp by simply clicking on the course. Screenshot 2022-04-23 130843.jpg

And also there is a list, that shows with how much XP someone would get which Level.

Screenshot 2022-04-23 131035.jpg

So I thought how can I get the best at english class without doing anything for it because that would be to easy (and also I am to lazy for that). Really I just wanted to get Master-Level because then you get a cool Sensei-Karate-Master-type dude as profile picture (that would spice up the schooltime a bit).

Then I thought of why don't I develop a Bot which clicks the course till I get Master-Level.

Realising my idea

Step 1: How can I realise my idea?

Now I needed to find a way to realise this amazing idea I had. I started searching in the web and found out that with Selenium someone could open the webbrowser and search things automatically. For implementing this I would use Python (because I first did not know it exists in Java).

Step 2: Start with Python Selenium

Install Library

First thing you need to do is of course install the library to use Selenium. I use 'pip' so I used the pip-command to install Selenium:

pip install selenium

Which Webbrowser will you use?

This is a question you must ask yourself before starting with Selenium because
a piece of your program depends on that. That is because Selenium needs a driver to open the Webbrowser. For everyone a different one. There is one for Chrome, Firefox and Microsoft Edge as I know.

If you want to use Chrome you need to install the chromedriver here. Just choose your OS and download the driver.

If you use Firefox you need the 'geckodriver'. I downloaded it on the official GitHub repository of Mozilla. When you went to the (Mozilla Repository) you just need to scroll down to the Assets of the latest release and just select your OS and download the driver.

Screenshot 2022-04-23 141207.jpg

I have absolute no idea what driver to use for Microsoft Edge and where to find it (but to be honest whoever uses MC Edge as his standard browser should rethink his life). In case you need it to make a program that can use every Webbrowser you should search in the WWW. Of course you can combine all the Webbrowsers by just trying out with 'try-except'.

Start implementing

So now just start watching tutorials or read the documentations of Selenium and build your Selenium knowledge.

First thing to do is to import the libraries.

from selenium import webdriver #Imports the Selenium Webdriver
from selenium.webdriver.common.by import By #This will be used to find elements 
import time

Now you create an instance that opens the Webbrowser (which is pretty easy).

driver = webdriver.Firefox("write here the path of your webdriver")
driver = webdriver.Chrome("write here the path of your webdriver")

You need to replace of course the string with the path where you unpacked your webdriver. For me it just was enough to have it in the same directory with my python-File.

Next is to try and open a website. In my case the first one was google.

driver.get('https://www.google.de/xhtml')

DISCLAIMER

At this point when you use Firefox the program could have an error but still funtion. Sometimes this Error occurs.

Error: firefox_profile has been deprecated, please pass in an Options object

In this case you need to import this:

from selenium.webdriver.firefox.service import Service
from selenium.webdriver.firefox.options import Options

And add this to your code:

#PROFILE_PATH -- For using Firefox as Browser
profile_path = r'C:\Users\...\AppData\Roaming\Mozilla\Firefox\Profiles\h9g5yzfj.default-release'
#OPTIONS
options=Options()
#OPTIONS FOR USING A CUSTOM PROFILE
options.set_preference('profile', profile_path)
#SPECIFY PATH of DRIVER geckodriver
service = Service(r'C:\Users\...\geckodriver.exe')
###==> Driver is set by ...= webdriver.Firefox(service=service, options=options)

Of course here you need to find your own profile-path by just clicking through the "\AppData\Roaming\Mozilla\Firefox\Profiles\" directory and insert at service the path of your own geckodriver.

Now change the opening of the Webbrowser to this:

driver = webdriver.Firefox(service=service, options=options)

And the Error should be terminated. And now back to implementing.

Find Elements and search things in Google

time.sleep(5)

#If you open Google they ask you to accept the terms and conditions 
tac = driver.find_element(By.ID, "L2AGLb") #The "I accept" button has the ID "L2AGLb"
#You can find the ID by expect the site/the element you want the ID from
#You can use also other ways to find elements --> By.Name,etc.
tac.click() #By this you can click the element you found and saved

#The name of the search bar of Google has the name 'q'
search_field = driver.find_element(By.NAME, 'q') #You save it in a variable
search_field.send_keys('hello there') #With send_keys you can write something in it
search_field.submit() #Submit it and you searched on Google

time.sleep(10) #I use time.sleep so that it is more like a real human surfing in the web
driver.quit() #At the end of your code you should close the website

So now back to my goal. At the .get-method I just put in the website where my school has its classes. Next I just needed to find the elements of the userform and logging in by submitting my userdata. Then I just clicked on the Course.

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.service import Service
from selenium.webdriver.firefox.options import Options
import time

#PROFILE_PATH -- For using Firefox as Browser
profile_path = r'C:\Users\...\AppData\Roaming\Mozilla\Firefox\Profiles\h9g5yzfj.default-release'
#OPTIONS
options=Options()
#OPTIONS FOR USING A CUSTOM PROFILE
options.set_preference('profile', profile_path)
#SPECIFY PATH of DRIVER geckodriver
service = Service(r'C:\Users\...\geckodriver.exe')
###==> Driver is set by ...= webdriver.Firefox(service=service, options=options)

driver = webdriver.Firefox(service=service, options=options)
driver.get("https://maybemyschoolwebsite.com")

username_field = driver.find_element(By.NAME, "username_input")
pwd_field = driver.find_element(By.NAME, "password_input")
login_btn = driver.find_element(By.CLASS_NAME, "btn-submit-login")

username_field.send_keys("username")
pwd_field.send_keys("password1234")
login_btn.click()
time.sleep(2)

course = driver.find_elements(By.CLASS_NAME, "english_course")
course.click()
time.sleep(5)

driver.quit()

After trying it multiple times I realised that you can claim the XP for clicking on the course only once a day. So my plans were crossed.

Changing my course

But now I did not want the time I invested to be wasted, so I decided to rick-roll my friends.

So I just searched on youtube the song and took the link. Then I put the link in my code and opened it. Because of the fact that Youtube wants you to accept their Cookies I am just waiting for it and click on I accept then.

Note: The "I accept"-string needs to be written in the code in the language of the Webbrowser

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time

driver = webdriver.Firefox()
#if you use Chrome use of course: driver = webdriver.Chrome()

#Opening the Youtube-Link
driver.get("https://www.youtube.com/watch?v=dQw4w9WgXcQ") 

#Letting the Webdriver just wait
wait = WebDriverWait(driver,30)

#Waiting until the accept-cookie site came up and clicking I accept
cookie = wait.until(EC.element_to_be_clickable((By.XPATH,"//yt-formatted-string[text()='I accept']")))
cookie.click()

#Waiting for the Song to play
time.sleep(180)

driver.quit()

Converting it to an exe-File

To conclude I used auto-py-to-exe which gives you a cool UI you can use for converting a py-File to an exe.

To use it first you need to install:

pip install pyinstaller

And then you need to install:

pip install auto-py-to-exe

If now you want to start auto-py-to-exe you just need to type:

auto-py-to-exe

in your console and now auto-py-to-exe opens a UI you can use.

Screenshot 2022-04-23 151658.jpg

Note: At the bottom you can see 'Current Command'. In Fact the library pyinstaller is the library to convert py-Files to exe-Files but auto-py-to-exe samples everything for you together so you don't need to learn everything about pyinstaller and just click together the properties of your exe-File and auto-py-to-exe does the rest for you.

Conclusion

At the end I named the exe-File 'runThis.exe' and let my friends click it to get rick-rolled. So all in all it was a success.

I hope this post helped you out in some way or maybe just entertained you. Thanks for reading

Adios

Ya fellow human Draithon