python - 使用 Selenium 和 Python 在 Chrome 中同时登录多个帐户
问题描述
我管理着我公司大约 150 个电子邮件帐户,并且我为 Selenium WebDriver 编写了一个 Python 脚本,它可以自动执行操作(删除垃圾邮件、清空垃圾箱……)一个又一个帐户,一天几次,而且它也是如此很慢,而且总是崩溃。我读到在 Amazon AWS 上使用 Docker 的 Selenium Grid 可以完成这项工作,而且 Selenium WebDriver 的“并行”选项似乎也可以。
我需要同时做的事情:
1)登录(所有帐户)
2)执行操作(删除垃圾邮件,清空垃圾箱,...)
3) 关闭 Chrome 实例
我目前必须使用 for 循环来创建 150 倍于我存储在列表中的相同指令,而这根本没有优化,它会使我的计算机崩溃......简而言之,我知道这不是要走的路我期待着让它同时并行运行。
这是我正在使用的代码的缩短版本:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
## GET THE USERS' EMAILS
emails = []
pwds = []
with open("users.txt", "r") as users_file: # Open the text file containing emails and pwds
for line in users_file:
email_and_pwd = line.split() # Extract the line of the .txt file as a list
emails.append(email_and_pwd[0]) # Add the email in the emails list
pwds.append(email_and_pwd[1]) # Add the pwd in the pwds list
nbr_users = len(emails) # number of users
## CREATE LISTS OF INSTRUCTIONS THAT WILL BE EXECUTED LATER AS CODE
create_new_driver = []
go_to_email_box = []
fill_username_box = []
fill_password_box = []
# Here I have the same type of lines to create lists that will contain the instructions to click on the Login button, then delete spams, then empty the trash, etc...
## FILL THE LISTS WITH INSTRUCTIONS AS STRINGS
for i in range(nbr_users):
create_new_driver_element = 'driver' + str(i) + ' = webdriver.Chrome(options = chrome_options)'
create_new_driver.append(create_new_driver_element)
# Here I have the same type of lines to create the rest of the instructions to fill the lists
## EXECUTE THE INSTRUCTIONS SAVED IN THE LISTS
for user in range(nbr_users):
exec(create_new_driver[user])
# Here I have the same type of lines to execute the code contained in the previously created lists
在浏览互联网几天后没有结果,任何形式的帮助表示赞赏。非常感谢 !
解决方案
我试图对@Jetro Cao 的回答发表评论,但评论太多了。我将根据他的工作做出回应。
为什么你的程序很慢?
正如@Jetro Cao提到的,您正在按顺序而不是并行运行所有内容。
另一个工具会让这更容易吗?
是的。如果可能,您应该使用电子邮件管理工具(例如:G Suite for gmail),而不是通过 python 脚本单独登录这些帐户。
将我的 150 个帐户凭据存储在文本文件中是否安全?
不,除非这些是没有任何敏感电子邮件的毫无意义的机器人帐户。
代码建议:
总体而言,您的代码有了一个良好的开端。几点具体意见:
- 避免使用 exec
- 使用@Jetro Cao推荐的线程
- 您没有向我们提供大部分代码。使用 selenium 时,可以使用一些技巧来帮助加快速度。一个例子是直接加载登录页面,而不是加载主页然后“单击”登录。
import threading
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
## GET THE USERS' EMAILS
emails = []
pwds = []
with open("users.txt", "r") as users_file: # Open the text file containing emails and pwds
for line in users_file:
email, pwd= line.split() # Extract the line of the .txt file as a list
emails.append(email) # Add the email in the emails list
pwds.append(pwd) # Add the pwd in the pwds list
nbr_users = len(emails) # number of users
## CREATE LISTS OF INSTRUCTIONS THAT WILL BE EXECUTED LATER AS CODE
drivers = []
go_to_email_box = []
fill_username_box = []
fill_password_box = []
# Here I have the same type of lines to create lists that will contain the instructions to click on the Login button, then delete spams, then empty the trash, etc...
## FILL THE LISTS WITH INSTRUCTIONS AS STRINGS
threads = []
for i in range(nbr_users):
t = threading.Thread(target=webdriver.Chrome, kwargs={'options': chrome_options})
t.start()
threads.append(t)
for t in threads:
t.join()
推荐阅读
- r - foreach 读取相同的 csv 会产生错误吗?
- jquery - 引导工具提示不会随着后退按钮单击和 AJAX 调用而消失
- ocr - 在 JPEG 图像中存储 OCR 数据/元数据的任何常规标准?
- mysql - 使用 MySQL 数据库的 AWS Lambda 中的 NodeJs IF 语句
- android - 我们如何在 NativeScript 中使用 Android 包 JAR
- jquery - 表单重置后如何添加/删除类并重新验证?
- python - Scrapy 使用大量计算机存储
- node.js - 测试响应正文(属性)给出断言错误 - Mocha、Chai、Node.js
- r - 如何制作由另一列约束的一列的记录数表
- java - centos8 install azkaban 加载类 `com.mysql.jdbc.Driver'。这已被弃用。新的驱动程序类是`com.mysql.cj.jdbc.Driver'