为什么我总是收到此脚本的运行时错误?

Why do I keep getting a Runtime Error with this script?

我意识到我假设 if__name__ == '__main__' 语句可以解决我的问题,但在搜索该站点后,这似乎是一个可能的答案...

我的脚本设计用于登录 Gmail 帐户、打开特定电子邮件并单击某个 link。然后脚本应该等待大约 15 秒,然后关闭新的 window,然后转到下一封电子邮件。

但是,当我尝试 运行 脚本时,它因运行时错误而卡住。

我将 post 脚本及其下方的错误消息。任何帮助将不胜感激,因为我完全被困住了。

# load modules
from multiprocessing.dummy import freeze_support
from socket import if_nameindex
from subprocess import call
from urllib.request import urlopen
from time import sleep as sleep
import selenium.webdriver.common.alert
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options as ChromeOptions
from selenium.webdriver.support import expected_conditions as EC
import undetected_chromedriver.v2 as uc
import pyautogui as pag
import random
import PIL

pag.PAUSE = 0.75
pag.FAILSAFE = True


# Setup undetected webdriver - Chrome
driver = uc.Chrome() # without options


# Login to Gmail
def gmail_login():
    driver.get('https://accounts.google.com/signin/v2/challenge/pwd?service=mail&passive=1209600&osid=1&continue=https%3A%2F%2Fmail.google.com%2Fmail%2Fu%2F0%2F%3Fhl%3Den-GB&followup=https%3A%2F%2Fmail.google.com%2Fmail%2Fu%2F0%2F%3Fhl%3Den-GB&hl=en-GB&emr=1&flowName=GlifWebSignIn&flowEntry=ServiceLogin&cid=1&navigationDirection=forward&TL=AM3QAYYUTnRXTuWE8Im5D6c1ck-DYnhDNwQZdU2z8S1Cp5HzEXXCQxi5OGQuA87M')
    driver.implicitly_wait(15)
    sleep(5)
    driver.maximize_window()
    sleep(3)

    # Gmail credentials
    username = "XXXX@gmail.com"
    password = "XXXXX"
    # find email input and insert email
    driver.find_element_by_xpath("/html/body/div[1]/div[1]/div[2]/div/div[2]/div/div/div[2]/div/div[1]/div/form/span/section/div/div/div[1]/div/div[1]/div/div[1]/input").send_keys(username)
    sleep(2)
    # click next button
    driver.find_element_by_xpath('//*[@id="identifierNext"]/div/button/span').click()
    # enter password
    sleep(2)
    passWordBox = driver.find_element_by_xpath(
        '//*[@id ="password"]/div[1]/div / div[1]/input')
    passWordBox.send_keys(password)
    sleep(2)
    nextButton = driver.find_elements_by_xpath('//*[@id ="passwordNext"]')
    nextButton[0].click()
    driver.implicitly_wait(10)
    sleep(35)
    promotions()

    if __name__ == '__main__':
        gmail_login()


# Select Promotion Tab
def promotions():
    x, y = pag.locateCenterOnScreen(r'C:\Users\New User\AppData\Local\Programs\Python\Python39 Second Traffic\promotions_tab.PNG', confidence=0.7)
    pag.click(x, y)
    sleep(3)
    email_open()
    
    
# Select Email To Open
def email_open():
    i = 0
    while i<30:
        x, y = pag.locateCenterOnScreen(r'C:\Users\New User\AppData\Local\Programs\Python\Python39 Second Traffic\earncredits.PNG', confidence=0.7)
        pag.click(x, y)
        sleep(3)
    
    # click credit link
        x, y = pag.locateCenterOnScreen(r'C:\Users\New User\AppData\Local\Programs\Python\Python39 Second Traffic\click_link_blue.PNG', confidence=0.7)
        sleep(3)
        driver.maximize_window()
        sleep(15)
        driver.close()

        i += 1
    else:
        sleep(3)
        print('Finshed on ' + i)
        sleep(2)
        # driver.quit()

错误信息:

File "C:\Users\New User\anaconda3\lib\multiprocessing\spawn.py", line 134, in _check_not_importing_main raise RuntimeError(''' RuntimeError: An attempt has been made to start a new process before the current process has finished its bootstrapping phase.

    This probably means that you are not using fork to start your
    child processes and you have forgotten to use the proper idiom
    in the main module:

        if __name__ == '__main__':
            freeze_support()
            ...

    The "freeze_support()" line can be omitted if the program

这似乎是一个 Windows/Mac 特定问题,请查看此 answer

这与这些平台上的多处理模块starts processes 的方式有关。基本上你不应该在 __main__ 上下文之外启动进程。

因此尝试将任何进程创建移动到 main 块中,如下所示:

# load modules
from multiprocessing.dummy import freeze_support
from socket import if_nameindex
from subprocess import call
from urllib.request import urlopen
from time import sleep as sleep
import selenium.webdriver.common.alert
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options as ChromeOptions
from selenium.webdriver.support import expected_conditions as EC
import undetected_chromedriver.v2 as uc
import pyautogui as pag
import random
import PIL

pag.PAUSE = 0.75
pag.FAILSAFE = True


# Setup undetected webdriver - Chrome
driver = None


# Login to Gmail
def gmail_login():
    driver.get('https://accounts.google.com/signin/v2/challenge/pwd?service=mail&passive=1209600&osid=1&continue=https%3A%2F%2Fmail.google.com%2Fmail%2Fu%2F0%2F%3Fhl%3Den-GB&followup=https%3A%2F%2Fmail.google.com%2Fmail%2Fu%2F0%2F%3Fhl%3Den-GB&hl=en-GB&emr=1&flowName=GlifWebSignIn&flowEntry=ServiceLogin&cid=1&navigationDirection=forward&TL=AM3QAYYUTnRXTuWE8Im5D6c1ck-DYnhDNwQZdU2z8S1Cp5HzEXXCQxi5OGQuA87M')
    driver.implicitly_wait(15)
    sleep(5)
    driver.maximize_window()
    sleep(3)

    # Gmail credentials
    username = "XXXX@gmail.com"
    password = "XXXXX"
    # find email input and insert email
    driver.find_element_by_xpath("/html/body/div[1]/div[1]/div[2]/div/div[2]/div/div/div[2]/div/div[1]/div/form/span/section/div/div/div[1]/div/div[1]/div/div[1]/input").send_keys(username)
    sleep(2)
    # click next button
    driver.find_element_by_xpath('//*[@id="identifierNext"]/div/button/span').click()
    # enter password
    sleep(2)
    passWordBox = driver.find_element_by_xpath(
        '//*[@id ="password"]/div[1]/div / div[1]/input')
    passWordBox.send_keys(password)
    sleep(2)
    nextButton = driver.find_elements_by_xpath('//*[@id ="passwordNext"]')
    nextButton[0].click()
    driver.implicitly_wait(10)
    sleep(35)
    promotions()


# Select Promotion Tab
def promotions():
    x, y = pag.locateCenterOnScreen(r'C:\Users\New User\AppData\Local\Programs\Python\Python39 Second Traffic\promotions_tab.PNG', confidence=0.7)
    pag.click(x, y)
    sleep(3)
    email_open()
    
    
# Select Email To Open
def email_open():
    i = 0
    while i<30:
        x, y = pag.locateCenterOnScreen(r'C:\Users\New User\AppData\Local\Programs\Python\Python39 Second Traffic\earncredits.PNG', confidence=0.7)
        pag.click(x, y)
        sleep(3)
    
    # click credit link
        x, y = pag.locateCenterOnScreen(r'C:\Users\New User\AppData\Local\Programs\Python\Python39 Second Traffic\click_link_blue.PNG', confidence=0.7)
        sleep(3)
        driver.maximize_window()
        sleep(15)
        driver.close()

        i += 1
    else:
        sleep(3)
        print('Finshed on ' + i)
        sleep(2)
        # driver.quit()

#bottom of page
if __name__ == '__main__':
    driver = uc.Chrome() 
    gmail_login()