'Python-Selenium-YearMonth-Increment-Submit

I have a question about Python 3.9.12 Selenium.

So far I coded via python selenium to perform actions stated below,

  1. auto login(as admin) a business email Website that I'm currently working as an IT Admin
  2. When I'm logged in, make selenium to order the webdriver, to get a email backup page, which only admins can access.
  3. The backup page GUI requires me to input the backupee's username, backup year,month(In YYYYMM Format), and to press submit button (For example If the Backupee has been working for 3years since 202001, I have to do this process multiple times without selenium.

#3 Kind of looks like this in a table form

Username YearMonth(YYYYMM) submitbutton
John Doe 201001 Submit

That's pretty much it and I was able to code it so far to make it to press the submit button after putting in all the infos that the backupwebpage requires,

But the problem that I'm encountering is, I can't find out how to

  1. make the code to do the same job but only increase 1month every time and Press submit

    • ex)1st input→Username(same)|202001|submit (1st selenium initialization)
    • ex)2nd input→Username(same)|202002|submit (automation)
    • .(automation)
    • .(automation)
    • ex)12th input→Username(same)|202012|submit (automation)
    • ex)13th input→Username(same)|202101|submit (automation)
  2. make selenium to stop when it reaches intended year month

    • ex) Intended YearMonth:202212 -26th input→202211 -27th input→202212 (stops because selenium reached its intended YearMonth)

Here's my full code, please forgive me that I can't actually include the website's link due to security reasons.

from datetime import datetime

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.firefox.service import Service
from webdriver_manager.firefox import GeckoDriverManager

import A_Info

#Variable Designation
driver = webdriver.Firefox(service=Service(GeckoDriverManager().install()))

#For Definition
def firstscr():
    driver.get('link')
    driver.maximize_window()
    driver.find_element_by_id("cid").send_keys(A_Info.id)
    driver.find_element_by_id('cpw').send_keys(A_Info.pw)
    driver.find_element_by_class_name('mb-auth-login-button').click() #Next Screen Transition
def secondscr():
    driver.get('link')
    driver.implicitly_wait(10)
def naming():
    driver.find_element_by_class_name('auto_complete-chosen').click()
    el1 = driver.find_element_by_class_name('auto_complete-input')
    el1.send_keys('John Doe') #input Backupee of your choice
    el1.send_keys(Keys.ENTER)
def date():
    date = datetime.strptime("2010,1,1","%Y,%m,%d")
    Mydate = date.strftime("%Y%m")
    driver.find_element_by_css_selector('#md_date').send_keys(Mydate)
def submit():
    driver.find_element_by_css_selector('#add_backup').click()

#For Activation 
firstscr()
secondscr()
naming()
date()
submit()

If there are any recommendations that I can try, please let me know. Thank you in advance.



Solution 1:[1]

Standard rule: to repeat something you need loop (for-loop or while-loop). (I skip recursion)

First: your function date() should get date directly as datetime

def date(new_date):
    Mydate = new_date.strftime("%Y%m")
    driver.find_element_by_css_selector('#md_date').send_keys(Mydate)

some_date = datetime.strptime("2010,1,1","%Y,%m,%d")
new_date(some_date)

or as string "202212"

def date(Mydate):
    driver.find_element_by_css_selector('#md_date').send_keys(Mydate)

some_date = datetime.strptime("2010,1,1","%Y,%m,%d")
Mydate = new_date.strftime("%Y%m")
new_date(Mydate)

(and frankly at this moment it could be simpler to put this directly in loop without function date() - but I skip it)

I had to use different names for variables because you can't have variable date and function date at the same time.

And now you can run in loop like

some_date = datetime.strptime("2010,1,1","%Y,%m,%d")

end = '202212'

firstscr()
secondscr()
naming()

while some_date.strftime("%Y%m") <= end:

    new_date(some_date)
    submit()

    some_date += datetime.timedelta(months=1)

I can't run it so I don't know if it need some changes. Maybe it needs to run naming() also in loop, or maybe it needs to load backup page again, etc.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 furas