0% found this document useful (0 votes)
2 views

Import Time

Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Import Time

Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

import time

from selenium import webdriver


from selenium.webdriver.common.by import By
import openai

# Set your OpenAI API key here


openai.api_key = "your-openai-api-key"

# Function to use OpenAI to generate a response based on the question


def generate_answer(question):
response = openai.Completion.create(
engine="text-davinci-003", # You can use other models too
prompt=question,
max_tokens=100 # Control the length of the answer
)
return response.choices[0].text.strip()

# Function to automate survey response using Selenium


def automate_survey(url):
# Initialize the Selenium WebDriver (adjust the path to the WebDriver)
driver = webdriver.Chrome(executable_path='C:\Users\user\Downloads\
chromedriver-win64.zip') # Change this path
driver.get(url) # Navigate to the survey page

# Wait for the page to load fully


time.sleep(2)

# Find all survey questions (assuming questions are in <input> or <textarea>)


question_elements = driver.find_elements(By.CSS_SELECTOR, 'input[type="text"],
textarea') # Modify this selector as needed

for question_element in question_elements:


# Extract question text (either from placeholder or aria-label)
question_text = question_element.get_attribute('placeholder') or
question_element.get_attribute('aria-label')

if question_text:
# Generate an answer using OpenAI API based on the extracted question
text
answer = generate_answer(question_text)
print(f"Question: {question_text}")
print(f"Answer: {answer}")

# Fill in the answer on the form field


question_element.send_keys(answer)
time.sleep(1) # Pause briefly between actions for safety

# Try to find the submit button and click it


try:
submit_button = driver.find_element(By.CSS_SELECTOR,
'button[type="submit"]')
submit_button.click()
print("Survey submitted.")
except Exception as e:
print(f"Error finding submit button: {e}")

time.sleep(2) # Wait before closing the browser


driver.quit()
# URL of the survey (replace with the actual survey URL)
survey_url = "https://example.com/survey" # Replace with the actual survey URL
automate_survey(survey_url)

You might also like