简体   繁体   中英

Is there a better way to make this code shorter and without repetition?

I am improving my code making it easier to read, short and more dynamic.

I got the username and password hidden using dotenv.

As you can see first it opens a driver and then in the function, I ask the driver to get into the URL, then depends what account I put in the parameter the driver will access that specific email.

I already think of assigning the driver actions in multiple variables, but since the web login has 2 steps;

  1. account and password
  2. encrypted password

it can't be done like that.

Any thoughts?

load_dotenv()


options = webdriver.ChromeOptions()
executable_path = {"executable_path": "/usr/local/bin/chromedriver"}
browser = Browser("chrome", **executable_path, headless=False)
bw = browser.driver

html = browser.html
soup = BeautifulSoup(html, "html.parser")


def open_web_email(account):
    """[This function will open web email]

    Arguments:
        account {[account]} -- [returns what email will open]
    """
    bw.get("https://webemail.com/login")
    bw.set_window_size(1080, 820)
    # username = bw.find_element_by_xpath('//*[@id="username"]')
    # password = bw.find_element_by_xpath('//*[@id="password"]')
    # login_butt = bw.find_element_by_xpath('//*[@id="login_btn"]')
    if account == "1":
        time.sleep(2)
        bw.find_element_by_xpath('//*[@id="username"]').send_keys(os.getenv("P_USERNAME_ONE"))
        bw.find_element_by_xpath('//*[@id="password"]').send_keys(os.getenv("P_PSW_ONE"))
        bw.find_element_by_xpath('//*[@id="login_btn"]').click()


        bw.find_element_by_xpath('//*[@id="mailboxPassword"]').send_keys(os.getenv("P_MAILBOX_ONE"))
        bw.find_element_by_xpath('//*[@id="unlock_btn"]').click()
    elif account == "2":
        bw.find_element_by_xpath('//*[@id="username"]').send_keys(os.getenv("P_USERNAME_TWO"))
        bw.find_element_by_xpath('//*[@id="password"]').send_keys(os.getenv("P_PSW_TWO"))
        bw.find_element_by_xpath('//*[@id="login_btn"]').click()


        bw.find_element_by_xpath('//*[@id="mailboxPassword"]').send_keys(os.getenv("P_MAILBOX_TWO"))
        bw.find_element_by_xpath('//*[@id="unlock_btn"]').click()

Maybe a better way to do it is using classes?

You can create another method and pass arguments to that method to make it shorter. Based on your code only arguments are different

load_dotenv()

options = webdriver.ChromeOptions()
executable_path = {"executable_path": "/usr/local/bin/chromedriver"}
browser = Browser("chrome", **executable_path, headless=False)
bw = browser.driver

html = browser.html
soup = BeautifulSoup(html, "html.parser")


def open_web_email(account):
    """[This function will open web email]

    Arguments:
        account {[account]} -- [returns what email will open]
    """
    bw.get("https://webemail.com/login")
    bw.set_window_size(1080, 820)
    if account == "1":
        time.sleep(2)
        createAccount(P_USERNAME_ONE,P_PSW_ONE,P_MAILBOX_ONE)
    elseif account == "2":
        createAccount(P_USERNAME_TWO,P_PSW_TWO,P_MAILBOX_TWO)


def createAccount(username,password,mailBox)
{

        bw.find_element_by_xpath('//*[@id="username"]').send_keys(os.getenv("username"))
        bw.find_element_by_xpath('//*[@id="password"]').send_keys(os.getenv("password"))
        bw.find_element_by_xpath('//*[@id="login_btn"]').click()


        bw.find_element_by_xpath('//*[@id="mailboxPassword"]').send_keys(os.getenv("mailBox"))
        bw.find_element_by_xpath('//*[@id="unlock_btn"]').click()

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM