Connecting to Gemini Web Chat using Selenium in Python

50 Views Asked by At

I'm trying to connect to the Gemini web chat using Selenium in Python to replicate the functionality described in this Medium article.

I've attempted to use the following code:

from selenium import webdriver 
from selenium.webdriver.firefox.options import Options 
from selenium.webdriver.common.by import By 
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC

def send_message(prompt): 
    options = Options() 
    options.headless = True 
    driver = webdriver.Firefox(options=options)

    try: 
        driver.get("https://gemini.google.com")
        # Assuming there is a login process, you may need to automate login here

        # Wait for the text area to be clickable and visible 
        text_area = WebDriverWait(driver, 10).until( 
            EC.element_to_be_clickable((By.CSS_SELECTOR, 'rich-textarea > div > p')) 
        ) 
        text_area.send_keys(prompt)

        # Find and click the send button 
        send_button = WebDriverWait(driver, 10).until( 
            EC.element_to_be_clickable((By.CSS_SELECTOR, 'div[class*="send-button-container"] > button')) 
        ) 
        send_button.click()

        # Wait for the response to appear 
        WebDriverWait(driver, 10).until( 
            EC.visibility_of_element_located((By.CSS_SELECTOR, 'message-content[class*="model-response-text"]')) 
        )

        # Find the response element 
        response_element = driver.find_element(By.CSS_SELECTOR, 'message-content[class*="model-response-text"]')

        # Get the text from the response element 
        response_text = response_element.text 
        return response_text 
    finally: 
        driver.quit()

def main(): 
    # Prompt user for what to ask Gemini 
    prompt = input("Ask Gemini: ")

    response_text = send_message(prompt)

    print("\nResponse from Gemini:") 
    print(response_text)

if __name__ == "__main__": 
    main()

However, it's throwing a TimeoutException with the following message: I'm not sure what is causing this issue. Any help or insights would be greatly appreciated.

selenium.common.exceptions.TimeoutException: Stacktrace: RemoteError@chrome://remote/content/shared/RemoteError.sys.mjs:8:8 WebDriverError@chrome://remote/content/shared/webdriver/Errors.sys.mjs:192:5 NoSuchElementError@chrome://remote/content/shared/webdriver/Errors.sys.mjs:510:5 dom.find/</<@chrome://remote/content/shared/DOM.sys.mjs:136:16

1

There are 1 best solutions below

0
OussR On

Check Browser Compatibility Ensure that the version of the Chrome WebDriver you are using is compatible with the version of Chrome browser installed on your system. Mismatched versions could lead to unexpected behavior or compatibility issues.