Automating ChatGPT with Python and Selenium

Zeeshan Ahmad
3 min readDec 29, 2022

--

ChatGPT is a chatbot that uses the GPT-3 language model to generate human-like responses to user input. It can be used for a variety of purposes, such as generating content, answering questions, and carrying on conversations.

One way to interact with ChatGPT is through the OpenAI API, which allows you to send and receive messages from the chatbot through an API. However, if you want to automate the process of interacting with ChatGPT, you can use a tool like Selenium, which allows you to control a web browser through code.

In this tutorial, we’ll go through the steps of setting up a Python script that uses Selenium to automate the process of interacting with ChatGPT.

Prerequisites

Before getting started, you’ll need to have the following tools installed:

  • Python 3
  • Selenium
  • ChromeDriver

You can install Python and Selenium by following the instructions on their respective websites. ChromeDriver is a separate executable that allows Selenium to control the Chrome browser. You can download the latest version of ChromeDriver from the ChromeDriver website.

Setting Up the Script

To start, let’s create a new Python script and import the necessary libraries:

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

Next, we’ll create a function that initializes the ChromeDriver and navigates to the ChatGPT website:

def start_chatbot():
driver = webdriver.Chrome()
driver.get('https://beta.openai.com/docs/api-reference/chatbot/interacting-with-chatgpt')

Now that we have the driver set up and the ChatGPT website loaded, we can start interacting with the chatbot.

Sending Messages to ChatGPT

To send a message to ChatGPT, we first need to find the input element where we can type our message. We can do this using the find_element_by_css_selector method:

our message. We can do this using the find_element_by_css_selector method:

input_element = driver.find_element_by_css_selector('input[type="text"]')

Once we have the input element, we can send our message by setting the value of the element and simulating the “enter” key press:

To find the message element, we can use the find_elements_by_css_selector method and select the elements with the message class:

messages = driver.find_elements_by_css_selector('.message')

This will return a list of elements, with the most recent message at the end of the list. We can access the text of the message using the text attribute of the element:

latest_message = messages[-1]
text = latest_message.text

Now that we have the text of the latest message, we can use it in our script or print it out to the console:

print(text)

Putting it All Together

Now that we’ve seen the individual steps, let’s put everything together into a single function that sends a message to ChatGPT and prints out the response:

def send_message(driver, message):
input_element = driver.find_element_by_css_selector('input[type="text"]')
input_element.send_keys(message)
input_element.send_keys(Keys.RETURN)
    time.sleep(3)    messages = driver.find_elements_by_css_selector('.message')
latest_message = messages[-1]
text = latest_message.text
print(text)

Conclusion

In this tutorial, we learned how to use Selenium and Python to automate the process of interacting with ChatGPT. With this script, you can easily send and receive messages from the chatbot and use the responses in your own projects.

I hope this helps! Let me know if you have any questions or need further clarification on any of the steps.

--

--

Zeeshan Ahmad

AI/ML/DL enthusiast | Python/Web Automation expert | Passionate Problem Solver