I'm stuck on finding the correct HTML attributes. Here’s the code.
import time
import pandas as pd
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.keys import Keys
input_file_path = 'random_questions.xlsx'
output_file_path = 'random_questions.xlsx'
questions_df = pd.read_excel(input_file_path)
# Use WebDriver
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
driver.get("https://copilot.microsoft.com/")
answers = []
for question in questions_df['Random Questions']:
# Clear cache
driver.delete_all_cookies()
driver.refresh()
# Wait for the page to load
time.sleep(5)
# Find the input box and enter the question
try:
label = driver.find_element(By.CSS_SELECTOR, 'label.text-input[data-input]')
driver.execute_script("arguments[0].setAttribute('data-input', arguments[1])", label, question)
# Simulate pressing the enter key
label.send_keys(Keys.RETURN)
except Exception as e:
answers.append(f"Error submitting question: {e}")
continue
# Wait for the answer to be generated
time.sleep(10)
# Retrieve the answer
try:
response_elements = driver.find_elements(By.CLASS_NAME, 'textarea#searchbox')
answer = " ".join([element.text for element in response_elements])
except Exception as e:
answer = f"Error retrieving answer: {e}"
answers.append(answer)
driver.quit()
# Save results to Excel
questions_df['Answers'] = answers
questions_df.to_excel(output_file_path, index=False)
print(f"Results saved to {output_file_path}")
After running the code below, I got this answer in Excel.
Error submitting question: Message: no such element: Unable to locate element: {"method":"css selector","selector":"label.text-input[data-input]"}