我意識到我假設這是if__name__ == '__main__'可以解決我的問題的宣告,但是在搜索了這個網站之后,這似乎是一個可能的答案......
我的腳本旨在登錄到 Gmail 帳戶,打開特定電子郵件并單擊某個鏈接。然后,該腳本應等待大約 15 秒,然后關閉新視窗,然后轉到下一封電子郵件。
但是,當我嘗試運行腳本時,它會遇到運行時錯誤。
我將發布腳本,然后在其下方發布錯誤訊息。任何幫助將不勝感激,因為我完全被卡住了。
# 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()
和錯誤資訊:
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
uj5u.com熱心網友回復:
這似乎是 Windows/Mac 特定的問題,請查看此答案
它與多處理模塊在這些平臺上啟動行程的方式有關。__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://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()
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/442320.html
標籤:python selenium-webdriver runtime-error
上一篇:無法找到第三個div元素
