Python-Senenium Yearth-Month-Muncrement-Submit

发布于 2025-01-26 01:55:48 字数 2658 浏览 1 评论 0原文

我有一个关于python 3.9.12硒的问题。

到目前为止

  1. ​电子邮件备份页面,只有管理员才能访问。
  2. 备份页GUI要求我输入backupee的用户名备份年,月份(以yyyymm格式),然后按 code>提交按钮(用于 我必须多次在没有硒的情况下进行

过程202001年以来一直在工作3年,
例如,如果备份自201001提交

这几乎是这样,我能够对其进行编码,以便在放入备份Webpage所需的所有Infos之后,按下提交按钮,

,但是我的问题是我'遇到的是 我找不到如何

  1. 使代码完成相同的工作,但每次都会增加1个月,然后按提交

    • ex)第1个输入→用户名(相同)| 202001 |提交(第1硒初始化)
    • ex)第二输入→用户名(相同)| 202002 |提交(自动化)
    • 。(自动化)
    • 。(自动化)
    • ex)第12个输入→用户名(相同)| 202012 |提交(自动化)
    • ex)第13个输入→用户名(相同)| 202101 |提交(自动化)
  2. 在达到预期的年度

    时,使硒停止
    • ex)预期的年度:202212 - 26输入→202211 - 第27个输入→202212(由于硒达到其预期的年度而停止)

这是我的完整代码,请原谅我,由于安全原因,我实际上不能包括网站的链接。

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()

如果我可以尝试任何建议,请告诉我。 预先感谢您。

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

UsernameYearMonth(YYYYMM)submitbutton
John Doe201001Submit

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拥抱影子 2025-02-02 01:55:48

标准规则:要重复某些内容,您需要循环( -loop或 -loop)。 (我跳过递归

首先:您的函数date()应直接获取日期为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)

或字符串“ 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)

(坦率地说,目前可以简单地将其直接放入没有函数date() - 但我跳过它的循环中,

我必须对变量使用不同的名称变量日期和函数日期同时。

现在,您可以像我无法运行一样循环运行

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)

,所以我不知道它是否需要一些更改。也许它需要在循环中运行naming(),或者也需要再次加载备份页面,等等。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文