返回

我可以在元组中存储方法调用而不在运行时调用方法吗?

发布时间:2022-07-04 03:52:58 256
# node.js

这个练习称为命令行电子邮件程序,它来自Automate the Boring Stuff with Python CH 12 2nd ed。

编写一个程序,在命令行上获取电子邮件地址和文本字符串,然后使用 selenium 登录到您的电子邮件帐户并将字符串的电子邮件发送到提供的地址。(您可能希望为此程序设置一个单独的电子邮件帐户。)

在完成这个练习时,我了解到用于查找 html 元素的 selenium 方法调用有时会起作用,但有时它们会抛出noSuchElementException. 为了解决这个问题,我创建了三个元组来存储 selenium 方法调用。使用该selenium_exception_loop()函数(见下文),我将遍历元组并一次调用一个方法。如果方法抛出noSuchElementFound异常,循环将等待两秒钟然后重试。

当然,问题在于 selenium 方法调用是在运行时执行的。有没有办法将 selenium 方法调用存储在集合中,而无需在运行时实际执行调用?还是我需要在这里采取完全不同的方法?

#!python3
# 04_command_line_emailer.py -- send emails from the command line
# usage: input four command line arguments:
# sys.argv[1] = email to log into
# sys.argv[2] = password
# sys.argv[3] = email body
# sys.argv[4] = recipient email address


from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException
import sys
import re
import time


class commandLineEmailer:
    def __init__(self):
        self.browser = webdriver.Firefox()
        self.gmail_method_calls = ()
        self.outlook_method_calls = ()
        self.yahoo_method_calls = (
            lambda: self.browser.find_element(By.CLASS_NAME, 'signin').click(),
            lambda: self.browser.find_element(By.NAME, 'username').send_keys(self.return_email_info()[0]),
            lambda: self.browser.find_element(By.NAME, 'signin').click(),
            lambda: self.browser.find_element(By.NAME, 'password').send_keys(sys.argv[2]),
            lambda: self.browser.find_element(By.NAME, 'verifyPassword').click(),
            lambda: self.browser.find_element(By.CSS_SELECTOR, 'Compose').click()
        )

    def return_email_info(self) -> tuple:
        '''
        Input sys.argv[1] into regex. sys.argv[1] contains the sending email
        address. return the username and the email client name.

        :return: tuple, username at index 0, email client name at index 1
        '''
        return re.compile("(.*)(\@)(.*)(\.)").match(sys.argv[1]).group(1), \
               re.compile("(.*)(\@)(.*)(\.)").match(sys.argv[1]).group(3)

    # open browser session and navigate to email client
    def go_to_email_client(self) -> None:
        EMAIL_CLIENTS = {
            'outlook': 'https://www.outlook.com/',
            'gmail': 'https://www.gmail.com/',
            'yahoo': 'https://yahoomail.com/'
        }
        self.browser.get(EMAIL_CLIENTS[self.return_email_info()[1]])

    def selenium_exception_loop(self, selenium_method_collection) -> None:
        '''
        :param selenium_method_collection: input collection containing selenium
         method calls to search for html elements. Wait two seconds between each
         method call. Except the NoSuchElementException error.
        :return: None
        '''
        for selenium_method_call in selenium_method_collection:
            while True:
                try:
                    time.sleep(2) # wait two seconds
                    selenium_method_call()
                    break
                except NoSuchElementException:
                    continue

    def sign_in_and_send(self) -> None:
        '''
        Retrieve the email client to log into. Call the selenium_exception_loop()
        function and pass it the email client tuple containing the selenium
        method calls
        :return None:
        '''
        if self.return_email_info()[1] == 'gmail':
            pass
        elif self.return_email_info()[1] == 'outlook':
            pass
        elif self.return_email_info()[1] == 'yahoo':
            self.selenium_exception_loop(self.yahoo_method_calls)

def main() -> None:
    command_line_emailer = commandLineEmailer()
    command_line_emailer.go_to_email_client()
    command_line_emailer.sign_in_and_send()

if __name__ == "__main__":
    main()
特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报
评论区(1)
按点赞数排序
用户头像