【发布时间】:2020-01-24 23:14:24
【问题描述】:
我是一个绝对的菜鸟,我有以下情况:我有一个 Excel 文件,其中有一列填充了 +4000 个 URL,每个 URL 位于不同的单元格中。该 url 链接到一个类似 facebook 的页面,在该页面中要求用户设置密码。我需要使用 Python 从列中检索每个 url,用 Chrome 打开它,为所有用户输入相同的指定密码,然后验证它是否登陆主页。
一步一步:
1 Openpyxl 打开 excel 电子表格。
2 查找带有 url 的列。
3 制作一个网址列表?
4 获取chrome打开第一个url。
6 查找密码字段。
7 输入一个对所有用户都相同的密码。
8 确认它登陆主页。
9 与列中的所有其他 url 循环直到结束。
10 如果有的话,最好得到一份报告来确认失败的次数。
到目前为止,这是我的代码:
# I can open the file
import openpyxl
wb=openpyxl.load_workbook('Test Sheet.xlsx')
type(wb)
# get the name of the sheet I need to work with
print (wb.sheetnames)
<Worksheet "Users">
# this line brings the current urls in my file
sheet=wb['Users']
for x in range (2,4):
print(x,sheet.cell(row=x,column=3).value)
# output
2 https://firstfacebookpage.com
3 https://secondfacebookpage.com
# I found this other way to retrieve the urls from the excel spreadsheet.
ws = wb['Users']
column = ws['c']
column_list = [column[x].value for x in range(len(column))]
print (column_list)
# output while having only 2 urls in the test sheet.
['Claim Link', 'https://somefacebookurl.com', 'https://someotherfacebookurl.com', None, None, None,
None, None, None, None, None, None, None, None, None, None, None, None, None, None]
# This login, enter password, verify, close browser, works perfectly if I manually enter the url.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome()
driver.get("https://firstfacebookpage.com")
password_box = driver.find_element_by_class_name('inputpassword')
password_box.send_keys("theonepassword")
print ("Password entered")
login_box = driver.find_element_by_id('u_0_9')
login_box.click()
print ("Done")
driver.close()
print("Finished")
现在我想不出一种方法来使“driver.get”成为电子表格中的 url 并遍历这些登录步骤。由于我的文件将在列中包含 +4000 个 URL,因此我宁愿让脚本为我执行此操作。 任何帮助都感激不尽。
【问题讨论】:
-
为什么需要使用 selenium 来读取 Excel 文件?你不能在没有 selenium 的情况下阅读它吗?使用python读取excel并将其存储到List/Array/Set中。
-
我正在使用 openpyxl 读取 excel 文件,我想我可以用它做一些事情,然后让 selenium 打开网址。我认为模块可以相互通信,但这可能是一个菜鸟的想法。
标签: python excel selenium loops url