简体   繁体   English

为多个 Gmail 帐户自动执行此任务/代码? (硒/蟒蛇)

[英]Automating this task/code for multiple Gmail accounts? (Selenium/python)

The code below I've made using some other sources, im new to python.下面的代码是我使用其他一些来源制作的,我是 Python 新手。 Anyways the below code runs and works fine.无论如何,下面的代码运行并正常工作。 This script will be deployed on a server and run by a scheduler and no human interaction is required.该脚本将部署在服务器上并由调度程序运行,不需要人工交互。 The issue I am having is, there are multiple emails that need this code to run on them.我遇到的问题是,有多个电子邮件需要此代码才能在其上运行。 How can I go about doing this?我该怎么做呢? What would be the best solution to do this?这样做的最佳解决方案是什么?

driver = webdriver.Chrome("C:\Program Files (x86)\chromedriver.exe",chrome_options=options)
driver.get("https://accounts.google.com/signin/v2/identifier?continue=https%3A%2F%2Fmail.google.com%2Fmail%2F&service=mail&sacu=1&rip=1&hl=en&flowName=GlifWebSignIn&flowEntry=ServiceLogin") #url


try: #if gb acount more than intended count
    driver.find_element_by_id("identifierId").clear()
    driver.find_element_by_id("identifierId").send_keys("email@gmail.com")
    driver.find_element_by_xpath("//div[@id='identifierNext']/div/button/div[2]").click()
    time.sleep(2)
    driver.find_element_by_name("password").clear()
    time.sleep(2)
    driver.find_element_by_name("password").send_keys("password")
    time.sleep(2)
    driver.find_element_by_xpath("//div[@id='passwordNext']/div/button/div[2]").click()
    time.sleep(2)
    gmailused =  driver.find_element_by_xpath("//*[@id=':8n']/div/div/div[1]/span[1]").text #xpath for used gb in gmail
    gmailtotal = driver.find_element_by_xpath("//*[@id=':8n']/div/div/div[1]/span[2]").text #xpath for total gb in gmail
    time.sleep(2)
    print("Used:", gmailused, "Total:", gmailtotal)
    time.sleep(2)
    if float(gmailused.rsplit(maxsplit=1)[0]) >= 0.00:
        time.sleep(2)
        print("GB Count is exceeded")
        driver.get("https://takeout.google.com/")
        driver.find_element_by_xpath("//div[@id='i6']/div/div/div[2]/button[2]/span").click()
        driver.find_element_by_name("Mail").click()
        time.sleep(5)
        driver.find_element_by_xpath("//div[@id='i6']/div/div/div[28]/div[2]/div[2]/div/button/span").click()
        driver.find_element_by_id("c0").click() 
        driver.find_element_by_xpath("//body[@id='yDmH0d']/div[8]/div/div[2]/span/div[4]/button/span").click()
        driver.find_element_by_xpath("//body[@id='yDmH0d']/div[8]/div/div[2]/span/div[4]/button[2]/span").click()
        driver.find_element_by_id("c15").click()
        driver.find_element_by_xpath("//body[@id='yDmH0d']/div[8]/div/div[2]/div[3]/div[2]/span/span").click()
        driver.find_element_by_xpath("//div[@id='i6']/div/div[2]/button/span").click()
        time.sleep(3)
        driver.find_element_by_xpath("//div[@id='i9']/div/div[2]/button/span").click()
        print ("Exported, check email for link")
        time.sleep(4)
        print("Exiting now...")
        driver.quit()
 
    else:
        #if gb count is less, browser close
        print("GB Count is not exceeding")
        time.sleep(3)
        print("Exiting now...")
        time.sleep(2)
        driver.quit()

One of the simple solutions to this is creating other functions that get executed once one is done.对此的一种简单解决方案是创建其他函数,这些函数一旦完成就可以执行。 You could create an array of WebDriver and use each one for a different email, however, a more effective solution to this could be using cookies, where you can bypass the login to each email, and not have to bother logging in or out.您可以创建一组 WebDriver 并将每个用于不同的电子邮件,但是,对此更有效的解决方案可能是使用 cookie,您可以在其中绕过对每封电子邮件的登录,而不必费心登录或注销。 If you have any questions regarding how to do that, let me know.如果您对如何执行此操作有任何疑问,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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