如何解决Python中的Try和Except Error?

How to solve Try and Except Error in Python?

提问人:ahmed osama 提问时间:11/15/2023 最后编辑:ahmed osama 更新时间:11/16/2023 访问量:57

问:

我编写了一个函数来重新启动 VPN,以便在发生验证码错误时更改我的 IP,但该函数用于任何错误,而不仅仅是验证码错误。

获取 Virustotal Captcha 的 HTML

captcha = wait.until(EC.element_to_be_clickable((driver.execute_script("return document.querySelector('vt-ui-shell').shadowRoot.querySelector('captchaContainer')")))) Virustotal 验证码 HTML

def restart_program(program_path, current_position, driver):
    # Save the current position to a file
    with open('restart_position.txt', 'w') as f:
        f.write(str(current_position))

    # Wait for 1 minute
    print("Waiting for 1 minute...")
    time.sleep(60)

    # Restart the program
    try:
        print(f"Restarting {program_path}")
        subprocess.run(["taskkill", "/f", "/im", os.path.basename(program_path)], check=True)
        subprocess.Popen([program_path], shell=True)
    except subprocess.CalledProcessError as e:
        print(f"Error: {e}")

    # Quit the browser
    driver.quit()

主要

if __name__ == "__main__":
    program_path = r"C:\Program Files\Cloudflare\Cloudflare WARP\Cloudflare WARP.exe"

    while True:
        try:
            driver = webdriver.Chrome()

            # Read the restart position from the file
            try:
                with open('restart_position.txt', 'r') as f:
                    restart_position = int(f.read())
            except FileNotFoundError:
                restart_position = 0

            with open('IOCs.txt', 'r') as file:
                IOCs = [line.strip() for line in file]

            for i, IOC in enumerate(IOCs[restart_position:], start=restart_position):
                perform_Scan(IOC)
python selenium-webdriver selenium-chromedriver

评论

0赞 Dean Van Greunen 11/15/2023
你不能绕过 reCapture,它不仅仅是点击一个按钮,“小部件”会监听你的鼠标移动,还有 cookie
0赞 Dean Van Greunen 11/16/2023
所以首先模拟一些随机的鼠标移动,(只需记录你自己的鼠标移动,然后在每次你想点击的时候回复它们)然后点击它
1赞 ahmed osama 11/16/2023
谢谢你的回复,院长。我不想绕过 Re-captcha。也许我用错误的方式解释了它。我需要的是修改条件,以便程序仅在错误仅与验证码相关而不是因为任何错误时重新启动
1赞 MatBailie 11/16/2023
调试引发的异常并捕获该异常。

答: 暂无答案