Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

网站登录成功后就卡顿了(Win11系统) #7

Open
michael7908 opened this issue Jan 5, 2024 · 1 comment
Open

网站登录成功后就卡顿了(Win11系统) #7

michael7908 opened this issue Jan 5, 2024 · 1 comment

Comments

@michael7908
Copy link

michael7908 commented Jan 5, 2024

具体描述:

运行write_response.py后,会弹出让我扫码Boss直聘的页面,然后扫码进入后就停留在

屏幕截图 2024-01-05 092933333

操作环境:Win11系统;网络环境:全局proxy。

报错讯息:

(venv) PS C:\Users*****\GPT-Projects\FreeSideProjects\auto_job__find__chatgpt__rpa\auto_job_find> python write_response.py
OpenAI version is compatible.
Loaded existing assistant ID.
Loaded existing assistant ID.

DevTools listening on ws://127.0.0.1:50547/devtools/browser/a*****-d868-4ed7-9605-91d8c12b7873
[2120:13720:0105/091745.511:ERROR:device_event_log_impl.cc(192)] [09:17:45.503] USB: usb_service_win.cc:104 SetupDiGetDeviceProperty({{A*****-DF1C-4EFD-0-6E0}, 6}) failed: 找不到元素。 (0x490)
An error occurred: Message:
Stacktrace:

    (No symbol) [0x00007FF*****]
    (No symbol) [0x00007FF6*****]
    (No symbol) [0x00007FF6*****]
    (No symbol) [0x00007FF6A*****97C]
    (No symbol) [0x00007FF6A*****7]
    (No symbol) [0x00007FF6A5*****F]
    (No symbol) [0x00007FF6A*****]
    (No symbol) [0x00007FF6*****]
    (No symbol) [0x00007FF*****]
    (No symbol) [0x00007FF6A*****]
    GetHandleVerifier [0x00007F*****B+3695259]
    GetHandleVerifier [0x00007*****37+4057191]
    GetHandleVerifier [0x0000*****5E4E3+4023827]
    GetHandleVerifier [0x00007*****304F9+689705]
    (No symbol) [0x0000*****048]
    (No symbol) [0x00007F*****044]
    (No symbol) [0x00007FF*****C9]
    (No symbol) [0x000*****C4]
    BaseThreadInitThunk [0x0*****+29]
    RtlUserThreadStart [0x000*****58+40]

之前也一直报"failed: 找不到元素。 (0x490)"这个错误,我咨询过GPT-4后,已经把write_response.py改成:

import json
import os
import time
from selenium.webdriver.support import expected_conditions as EC

import openai
from openai import OpenAI
from selenium.webdriver import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait

import functions
import finding_jobs

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager

Check OpenAI version compatibility

from packaging import version
from dotenv import load_dotenv
load_dotenv()

required_version = version.parse("1.1.1")
current_version = version.parse(openai.version)
OPENAI_API_KEY = os.getenv('OPENAI_API_KEY')
if current_version < required_version:
raise ValueError(
f"Error: OpenAI version {openai.version} is less than the required version 1.1.1"
)
else:
print("OpenAI version is compatible.")

Initialize OpenAI client

client = OpenAI(api_key=OPENAI_API_KEY)

Create or load assistant

assistant_id = functions.create_assistant(
client) # this function comes from "functions.py"

def create_thread(client):
# Function to create a new thread and return its ID
try:
response = client.beta.threads.create() # No assistant_id needed
thread_id = response.id
return thread_id
except Exception as e:
print(f"Error creating thread: {e}")
return None

def open_browser_with_options(url, browser_type):
# 假设这是您原始代码中的函数,用于初始化 WebDriver

if browser_type == "chrome":
    chrome_options = Options()
    # 忽略 SSL 证书错误
    chrome_options.add_argument('--ignore-certificate-errors')
    # 指定 WebDriver 路径
    service = Service(ChromeDriverManager().install())
    driver = webdriver.Chrome(service=service, options=chrome_options)
else:
    # 如果有其他浏览器类型的处理
    pass

driver.get(url)
return driver

def chat(user_input, assistant_id, thread_id=None):
if thread_id is None:
thread_id = create_thread(client)
if thread_id is None:
return json.dumps({"error": "Failed to create a new thread"})

print(f"Received message: {user_input} in thread {thread_id}")

# Run the Assistant
try:
    # Add the user's message to the thread
    client.beta.threads.messages.create(
        thread_id=thread_id,
        role="user",
        content=user_input
    )

    # Start the Assistant Run
    run = client.beta.threads.runs.create(
        thread_id=thread_id,
        assistant_id=assistant_id
    )

    # Check if the Run requires action (function call)
    while True:
        run_status = client.beta.threads.runs.retrieve(
            thread_id=thread_id,
            run_id=run.id
        )

        if run_status.status == 'completed':
            break
        elif run_status.status == 'requires_action':
            # Here you can handle specific actions if your assistant requires them
            # ...
            time.sleep(1)  # Wait for a second before checking again

    # Retrieve and return the latest message from the assistant
    messages = client.beta.threads.messages.list(thread_id=thread_id)
    assistant_message = messages.data[0].content[0].text.value

    # 将换行符替换为一个空格
    formatted_message = assistant_message.replace("\n", " ")

    # response_data = json.dumps({"response": assistant_message, "thread_id": thread_id})
    return formatted_message

except Exception as e:
    print(f"An error occurred: {e}")
    error_response = json.dumps({"error": str(e)})
    return error_response

def send_response_to_chat_box(driver, response):
# 定位聊天输入框
chat_box = driver.find_element(By.XPATH, "//*[@id='chat-input']")

# 清除输入框中可能存在的任何文本
chat_box.clear()

# 将响应粘贴到输入框
chat_box.send_keys(response)
time.sleep(3)

# 模拟按下回车键来发送消息
chat_box.send_keys(Keys.ENTER)
time.sleep(1)

def send_response_and_go_back(driver, response):
# 调用函数发送响应
send_response_to_chat_box(driver, response)

time.sleep(10)
# 返回到上一个页面
driver.back()
time.sleep(3)

def send_job_descriptions_to_chat(assistant_id, url, browser_type, label):
# 开始浏览并获取工作描述
finding_jobs.open_browser_with_options(url, browser_type)
finding_jobs.log_in()

job_index = 1  # 开始的索引
while True:
    try:
        # 获取 driver 实例
        driver = finding_jobs.get_driver()

        # 更改下拉列表选项
        finding_jobs.select_dropdown_option(driver, label)
        # 调用 finding_jobs.py 中的函数来获取描述
        job_description = finding_jobs.get_job_description_by_index(job_index)
        if job_description:
            # 发送描述到聊天并打印响应
            response = chat(job_description, assistant_id)
            print(response)
            time.sleep(1)
            # 点击沟通按钮
            contact_button = driver.find_element(By.XPATH, "//*[@id='wrap']/div[2]/div[2]/div/div/div[2]/div/div[1]/div[2]/a[2]")
            contact_button.click()

            # 等待回复框出现
            xpath_locator_chat_box = "//*[@id='chat-input']"
            chat_box = WebDriverWait(driver, 10).until(
                EC.presence_of_element_located((By.XPATH, xpath_locator_chat_box))
            )

            # 调用函数发送响应
            send_response_and_go_back(driver, response)



        # 等待一定时间后处理下一个工作描述
        time.sleep(3)
        # job_index += 1

    except Exception as e:
        print(f"An error occurred: {e}")
        break

if name == 'main':
assistant_id = functions.create_assistant(client)
url = "https://www.zhipin.com/web/geek/job-recommend?ka=header-job-recommend"
browser_type = "chrome"
label = "AI产品经理" # 想要选择的下拉菜单项
send_job_descriptions_to_chat(assistant_id, url, browser_type,label)

如何解决,还望告知。谢谢。

@Frrrrrrrrank
Copy link
Owner

你好,这个问题是因为账号不同,我发现有些账号是在AI产品经理那里是下拉菜单,有些则是最多选择三个标签后并排显示,这会导致html的xpath不同,详情可以看下面的链接:

#1

你可以先尝试自己改一下,上述链接里有大体方法,然后要是还有问题可以再在这问我,主要我账号上不是这么显示的,所以我也没法改....你那里改好以后可以把更改部分的代码分享在这里吗?我到时候也可以更新一下相关文件来让他的适配性更好一些

Repository owner deleted a comment from loks666 Apr 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants