In this post, I will show you how to create a simple script using Python and Selenium to automate inputs and outputs from Google’s Bard. This assumes you have been given access to Bard. If not, you may sign up for the waiting list here: [provide helpful link]
Prerequisites
- Python 3
- Selenium
- Chrome WebDriver
Downloading ChromeDriver
First, we must download the ChromeDriver that works with the version of Google Chrome you have installed. To do this:
- Open an instance of Google Chrome
- Click on the vertical 3 dots in the upper right corner
- Select Help > About Google Chrome
The next step is to download the ChromeDriver.exe. The ChromeDriver may be downloaded from here: https://chromedriver.chromium.org/downloads. Be sure to select the correct version. Download and unzip it into a known location on your hard drive.
In this example, our version of Chrome is 112.
Troubleshooting
If using Mac and you receive the popup notification “Error: “chromedriver” cannot be opened because the developer cannot be verified. Unable to launch the chrome browser” You can try the following steps:
- Download ChromeDriver and unzip to project directory
- Using Terminal, navigate to the project directory containing ‘chromedriver’
- Enter the following command: spctl –add –label ‘Approved’ chromedriver
- Enter your password into the spctl dialog box that opens
- Now your chromedriver has been added to the ‘Approved’ list
NOTE: If you download another copy or different version of the chromedriver you will need to execute these steps again. Every file has a different signature and Mac will treat any new chromedriver files as ‘Un-Approved’ files.
Getting Access
Google’s Bard has limited access that requires you to be signed into the Google account that was granted access to it. Using a Python script to automate the actions of logging into a Google account is time consuming and unnecessary for this post. Instead, we will be manually logging into the Google account, accessing BARD and then copying the authentication cookie. We will then use the cookies’ name and value in our script to gain access.
Getting Cookie Value
Using Google Chrome Browser
- Navigate to https://bard.google.com/
- Using your Google Login, click the sign in button in the upper right corner
- Sign into the Google account that was granted access to use Bard
- Open the Developer’s panel (Press F12, or navigate using menus: Click 3 dots in upper right > More Tools > Developer Tools)
- On the bar in the Developer Tools panel, select “Application” (you may need to click the >> button to reveal more items)
- Now, in the left panel under the Application tab, expand the Cookies tree
- Select the cookie item labeled “https://bard.google.com”
You will see a table with containing information about every cookie that site uses.
Select the line for the cookie named “__Secure-1PSID”. Right click the value for this cookie and copy. This cookie value will be pasted into the Python code below.
Creating The Python Script
# Python code generated by AutomateBard.com
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
import time
COOKIE_NAME = "__Secure-1PSID"
COOKIE_VALUE = "<paste cookie value here>"
def search_bard(web_driver, search_string):
search_bar = web_driver.find_element("id", "mat-input-0")
search_bar.clear()
# Enter Search String
search_bar.send_keys(search_string)
# Submit Search
search_bar.send_keys(Keys.ENTER)
# Allow time for Bard to respond
time.sleep(10.0)
# Grab response and return
return web_driver.find_element(By.CLASS_NAME, "model-response-text").text
# Main Script
if __name__ == '__main__':
try:
print("Connecting to ChromeDriver")
driver = webdriver.Chrome('./chromedriver')
driver.implicitly_wait(1.0)
print("Connecting to dummy site")
driver.get("https://bard.google.com/u/1/")
print("Adding cookie")
driver.add_cookie({
"name": COOKIE_NAME,
"value": COOKIE_VALUE
})
print("Transferring to Bard site")
driver.get("https://bard.google.com/u/1/")
response = search_bard(driver, "How are you, today?")
print("Bard Replied:", response)
except Exception as e:
# DO NOT DO THIS. Use proper exception handling!
print(e)
finally:
print("Closing ChromeDriver")
driver.close()
TroubleShooting
Sometimes errors will occur. I will list some errors I’ve encountered during the development process and try to provide some insight into what is causing them.
Error 1:
Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="mat-input-0"]"}
(Session info: chrome=112.0.5615.121)
This is caused by a failed login to the Google account Bard is being access through. If the sign in fails, the prompt for Bard will not be available for Selenium to find. In this case, Selenium is trying to find the CSS element with the id=”mat-input-0″. This was caused by a wrong or missing value for the cookie variable in the script.
This works great! Thank you!