使用 (Python, Selenium) 在 ProtonMail 上自动创建电子邮件

Automating email creation on ProtonMail using (Python, Selenium)

我想在 ProtonMail 上创建 10 封电子邮件。

我已经使用 PyAutoGuiSelenium 自动化了一半的部分,但我想让它像一个检查器一样,因为有时用户名会被占用。 现在我想做的是:

这就是我现在想要构建的概念。如果我听起来很垃圾,我真的很抱歉,但我几天前才开始使用 Python,现在还不到一周,所以...我正在学习 :P

我已经自动化了 Selenium 填写 ProtonMail 表格以进行登录/注册的部分,但有时我会收到用户名已被占用的错误,我希望脚本检查是否弹出该错误消息,如果是,将执行 "reserved code" 行来解决问题。然后,代码可以继续。但是,如果元素没有弹出,我希望脚本不会干扰 "reserved code"。

如果有人只是为了准备好代码,请开始:

import selenium
import pyautogui
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

#Variables
protonmail = "https://mail.protonmail.com/create/new?language=en"

username = input("Please enter your desired username for the email:")
password = input("Enter your password:")

driver = webdriver.Firefox()
time.sleep(4)
driver.get(protonmail)
time.sleep(7)

pyautogui.click(535, 501)
time.sleep(1)
pyautogui.typewrite(username)
time.sleep(2)
driver.find_element_by_xpath(
    "/html[1]/body[1]/div[2]/div[1]/div[1]/div[1]/div[1]/form[1]/div[1]/div[2]/div[1]/div[1]/input[1]").send_keys(
    password)
time.sleep(2)
driver.find_element_by_xpath(
    "/html[1]/body[1]/div[2]/div[1]/div[1]/div[1]/div[1]/form[1]/div[1]/div[2]/div[2]/div[1]/input[1]").send_keys(
    password)
time.sleep(2)
pyautogui.click(1284, 916)
time.sleep(2)
pyautogui.click(655, 762)
time.sleep(3)

我刚刚检查了 ProtonMail 注册页面以找到您正在谈论的错误消息。根据您的问题描述,您似乎希望根据此错误消息的存在在您的代码中进行不同的处理。你的代码示例有点难以阅读,因为你的点击都是在绝对坐标上,而不是实际的 WebElements,所以我不完全确定在你的示例中点击了什么。这对您来说可能是一个很好的起点:

from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


# Fill in all details on form
# Click Create Account - I assume you have already done these two steps

# Check for error message -- this is in an iframe

# switch to first iframe on the page which will contain the error message
iframe = driver.find_elements_by_xpath("//iframe[@title='Registration form']")[0]

# attempt to find the error message, catch the exception if it does not exist
try:
    # Handle scenario where error message exists -- username is taken
    error_message = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//p[text()='Username already used']")))

    # better to write a handler method, instead of stuffing code into the try block
    call_some_handler_method_here()

except TimeoutException:
    # Handle scenario where error message does NOT exist -- meaning, username is not taken
    call_some_other_handler_method_here()

此代码将切换到 iframe,其中包含用户名已被占用的错误消息——这是驱动程序定位元素所必需的。之后,在try/except块中引入WebDriverWait来检查错误信息是否存在。

如果出现错误消息,您将在 try 块内结束,您可以在其中调用相应的方法进行处理。

如果错误消息不存在,意味着用户名未被使用,您将在 except 块中结束,您可以在其中调用不同的方法进行相应的处理(例如保存尝试的用户名到一个文件)。

您可以使用请求库来检查用户名是否有效:

import requests

URL = "https://mail.protonmail.com/api/users/available"
PARAMS = {"Name": "UsernameToCheck"}

# idk what these are but it seems like they are needed
HEADERS = {"x-pm-appversion": "Web_3.16.17",
          "x-pm-apiversion": "3"}

r = requests.get(url=URL, params=PARAMS, headers=HEADERS)

if int(r.json()["Code"]) == 1000:
    print("valid username")

else:
    print("invalid username")