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

Time:03-06

I realise that I am presupposing it is the if__name__ == '__main__' statement that will fix my problem, but after searching this site this seems to be a likely answer...

My script is designed to login to a Gmail account, open specific emails and click on a certain link. The script should then wait about 15 seconds, before closing the new window, and then move onto the next email.

However, when I try to run the script it gets stuck with a Runtime Error.

I will post the script and then the error message below it. Any help would be greatly appreciated, because I am completely stuck.

# 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://mail.google.com/mail/u/0/?hl=en-GB&followup=https://mail.google.com/mail/u/0/?hl=en-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 = "[email protected]"
    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\60 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\60 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\60 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()

And the error message:

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

CodePudding user response:

It seems to be a Windows/Mac specific issue, check out this answer

It has to do with the way the multiprocessing module starts processes on those platforms. Basically you should not start processes outside the __main__ context.

So try to move any process creation into the main block as shown below:

# 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://mail.google.com/mail/u/0/?hl=en-GB&followup=https://mail.google.com/mail/u/0/?hl=en-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 = "[email protected]"
    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\60 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\60 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\60 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()
  • Related