在 Python 中使用 Selenium 连接到 Gemini Web Chat

问题描述 投票:0回答:1

我正在尝试使用 Python 中的 Selenium 连接到 Gemini 网络聊天,以复制这篇 Medium 文章中描述的功能。

我尝试使用以下代码:

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()

但是,它会抛出

TimeoutException
并显示以下消息:我不确定是什么导致了此问题。任何帮助或见解将不胜感激。

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

python openai-api google-api-python-client chatgpt-api google-gemini
1个回答
0
投票

检查浏览器兼容性确保您使用的 Chrome WebDriver 版本与系统上安装的 Chrome 浏览器版本兼容。版本不匹配可能会导致意外行为或兼容性问题。

© www.soinside.com 2019 - 2024. All rights reserved.