Implementing a Simple Bot to Swipe on OkCupid

Darryl See Wei Shen
Analytics Vidhya
Published in
5 min readApr 10, 2020
https://www.businessofapps.com/news/dating-app-download-growth-slumps-to-5-3-in-2019/

Preamble

It is no doubt that we will feel lonely midst the COVID-19 pandemic. Needing to serve quarantine, practising social distancing, cities being locked down, mandatory working/school from home are some precautionary measures leaders from around the world are taking to curb the spread of this virus. It took a toll on our social interactions with people. Thankfully, online dating apps, like Tinder, OkCupid and Bumble to name some, exist. It enables us to meet and interact with new people from the comforts of our home.

Agenda

Building an OkCupid Bot with Python, Selenium and Chromedrive.exe.

Motivation

Recently, I met this girl on OkCupid (Singapore) and she gave me a challenge, to search for two of her friends on OkCupid, in exchange for her Instagram handle by 092359H Apr 2020. I gladly accepted the challenge.

I started brainstorming for possible solutions to the challenge.

It is worth noting that I was busy with work and studies, simply scrolling through every possible match on the app is inefficient and time-consuming. Given the circumstances, I decided to build a bot.

Prerequisites and Configurations

  1. Have Python (3.X and above is recommended), Selenium and Chromedriver.exe installed.

Please download the correct version of Chromedriver.exe for your version of chrome (Settings > About Chrome). I am using Chrome Version 81.0.4044.92 (Official Build) (64-bit).

Python 3.7.6
selenium 3.141.0 (pip install selenium)
ChromeDriver 81.0.4044.69

2. Add Chromedriver.exe to PATH variable. Type chromedriver.exe into your command prompt (Windows) or terminal (macOS), if it opens a local session, you’re all set, else, there is an error.

3. Basic knowledge about Object Oriented Programming (OOP).

Walkthrough

Remember this, SELECT and INPUT. These are the two basic operations you MUST remember when automating a web. Imagine you are the one using the app, then translate your actions to codes.

Code

Start by importing these packages.

from selenium import webdriver
import time

Create a Bot Class

class OKCBot():
def __init__(self):
chrome_options = webdriver.ChromeOptions() chrome_options.add_experimental_option("excludeSwitches", ['enable-automation'])
self.driver = webdriver.Chrome(options=chrome_options)

This removes the developers’ restrictions from chrome, else chrome will prompt “Chrome is being controlled by automated test software.”

The Bot will take in several functions as listed below:

  1. Starts a session and completes the login procedures.
def open(self):
self.driver.get('https://www.okcupid.com/home')
def sign_in(self):
time.sleep(3)
email_input = self.driver.find_element_by_xpath('//*[@id="username"]')
password_input = self.driver.find_element_by_xpath('//*[@id="password"]')
email_input.send_keys('someEmail@someDomain.com')
password_input.send_keys('somePassword')
next_btn = self.driver.find_element_by_xpath('//*[@id="OkModal"]/div/div[1]/div/div/div/div[2]/div/div/div[2]/div/form/div[2]/input')
next_btn.click()

2. Filter the profiles by name (This is optional, you could make the Bot swipe right/like on every profile it encounters).

def nameChecker(self):
time.sleep(5)
name = self.driver.find_element_by_xpath('//*[@id="quickmatch-wrapper"]/div/div/span/div/div[2]/div/div[2]/span/div/div/div/div[1]/div[1]/div[1]').text
if name in ['Rachel', 'hanna']:
self.swipeRight()
print(name, ' Liked')
else:
self.swipeLeft()
print(name, ' Passed')

3. swipeRight and swipeLeft functions.

def swipeRight(self):
time.sleep(1)
like_btn = self.driver.find_element_by_xpath(
'//*[@id="quickmatch-wrapper"]/div/div/span/div/div[2]/div/div[2]/span/div/div/div/div[1]/div[2]/button[2]/div')
like_btn.click()
def swipeLeft(self):
time.sleep(1)
pass_btn = self.driver.find_element_by_xpath(
'//*[@id="quickmatch-wrapper"]/div/div/span/div/div[2]/div/div[2]/span/div/div/div/div[1]/div[2]/button[1]/div')
pass_btn.click()

Code Explanations

You can completely skip this part if you understand the methods used.

Pseudocode for Bot Functions

When you browse the web, you will typically SELECT or INPUT where applicable. These are step-by-step (refer to Pseudocode Flowchart) instructions you need to define for the Bot. I will describe the processes involved.

Actions Flowchart

SELECT

someVariable = driver.find_element_by_xpath(*arg)
# Many for Methods

There are a whole bunch of other find_element_by methods to located elements within the HTML script, but for this article, I will use xpath of an element.

SELECT Flowchart

The Bot will shift its focus to that particular element similar to how we hover our mouse over an area of interest on the browser.

INPUT

Login Page
someVariable.send_keys('someString')
# Input text into HTML input boxes

This method replicates the inputting action, keying in login information, filling up a form etc.

Click Action

Next Button on Login Page
next_btn = self.driver.find_element_by_xpath(*arg)
next_btn.click()

This executes a particular action, defined by the developer, in this case, it’s a “Click” action. This is similar to you manually clicking the submit button in the login page or pass/like buttons.

Store the element in a variable and execute the .click() method on the variable.

Check out this documentation for comprehensive coverage of the types of actions you can do with Selenium!

Running the Bot

Instantiate the Bot, call its functions, sit back and relax.

# Instantiate Bot
bot = OKCBot()
# Start Session
bot.open()
# Sign-In
bot.sign_in()
# Swiping Left or Right
while True:
bot.nameChecker()

There you have it, a functional OKCBot().

Problems I ran into and my solutions

  1. Being careless.
driver.find_elements_by_xpath(*arg)

This method returns a list. It is typically used to find all elements that fit the selector argument. Did not realise I have been typing an extra ‘s’ in the method, don’t be careless. Check your script, check the documentation.

2. Chrome did not allow automation.

chrome_options = webdriver.ChromeOptions()        chrome_options.add_experimental_option("excludeSwitches", ['enable-automation'])
driver = webdriver.Chrome(options=chrome_options)

A simple quick fix to remove default settings in Chrome.

Moving Forward

I have plans to automate chatting with matches in future by implementing a simple AI chatbot and also using image analysis to make the pass and like decision.

Conclusion

I hope this tutorial article is sufficed to get you started with building bots and automating webpages! Please feel free to comment on your opinions or connect with me!

PS: She did not give me her Instagram handle, but I learned something new, that’s all that matters.

Source Code: https://github.com/peanutsee/OKCBOT

--

--

Darryl See Wei Shen
Analytics Vidhya

Student | Programming | Data Analysis | Wakeboard | Nerding